1

My company is serving up PPC landing pages with Unbounce (on a subdomain), which then links back to our website. We're using the following code to append the AdWords gclid variable to outgoing links:

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

The issue arises when the Gclid gets appended to a URL that already has a parameter (like our checkout forms). You end up with a URL that looks like this:

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

I'm wondering if there's a way to change the Glid's '?' to an '&' for certain URLs that already have parameters, while keeping the existing functionality of using the '?' for the others.

Thanks!

Edit: Seems like there's some redirection going on, this is how the href looks for the links in question: http://domain.com/lp/clkg/https/domain.com/cart.aspx?EventID=125160?gclid=CPfO1JaOx7oCFQZyQgod5A4AFw

asked Nov 2, 2013 at 20:28

1 Answer 1

1

Simply replace it yourself:

$(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);
 }
 });
});

Edit: and are you aware that you are replacing subsequent spaces with only one single '%20'?

answered Nov 2, 2013 at 20:35
Sign up to request clarification or add additional context in comments.

6 Comments

Hm, I tried using your fix above and the GClid stopped getting passed at all, so the destination URL only contains the cart parameter.
I had one typo ('&' . params.substr(1) should be '&' + params.substr(1) of course), for the rest it should work: jsfiddle.net/TJ5p5
Yeah, that was it. Can confirm the above snippet works like a charm!
That's the problem of programming in both PHP (where string concatenation is done with a dot) and in JavaScript (where string concatenation is done with a plus-sign). This wasn't the first time I made that fault ;)
Hey Peter, just to follow up, what's the issue with the spaces? This is code I got from Unbounce so I'm not sure what the issue is (if there is one).
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.