根据目标更改URL参数附加到链接的方式

时间:2013-11-02 20:28:21

标签: javascript jquery google-analytics

我的公司正在使用Unbounce(在子域上)提供PPC登陆页面,然后链接回我们的网站。我们使用以下代码将AdWords gclid变量附加到外发链接:

 $(document).ready(function() {var params = window.location.search.replace(/\+/g,'%20'); var button = $('a').each( function(i) {this.href = this.href + params;});});

当Gclid被附加到已经有参数的URL(比如我们的结账表单)时会出现问题。您最终会得到一个如下所示的网址:

domain.com/checkout?EventID=15546?Gclid=jdkI324kd 

我想知道是否有办法将Glid的''更改为已经拥有参数的某些网址的'& ',同时保留现有的使用'?'的功能为了其他人。

谢谢!

编辑:似乎有一些重定向正在进行,这就是href查找相关链接的方式: http://domain.com/lp/clkg/https/domain.com/cart.aspx?EventID=125160?gclid=CPfO1JaOx7oCFQZyQgod5A4AFw

1 个答案:

答案 0 :(得分:0)

只需自己更换:

$(document).ready(function() {
    var params = window.location.search.replace(/\+/g,'%20'); 
    var button = $('a').each( function(i) {
        if (this.href.indexOf('?') == -1) {
            this.href = this.href + params;
        } else if (params.length) {
            this.href = this.href + '&' + params.substr(1);
        }
    });
});

编辑:您是否知道您只用一个'%20'替换后续空格?