AdWords
5.4K members online now
5.4K members online now
Use AdWords conversion tracking and reporting to measure your results. Have a question about Google Analytics? Post it here, on the Google Analytics Community!
Guide Me
star_border
Reply

Adwords GCLID tracking - Cookie won't set

[ Edited ]
Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

I'm trying to set up GCLID tracking so I can import offline conversions.  However, I'm being completely stymied before I even start. 

 

I've input the code precisely as Google says, at the very end of all pages right before the </body> tag.

 

<script type="text/javascript">
function setCookie(name, value, days){
    var date = new Date();
    date.setTime(date.getTime() + (days*24*60*60*1000));
    var expires = "; expires=" + date.toGMTString();
    document.cookie = name + "=" + value + expires + ";path=/";
}
function getParam(p){
    var match = RegExp('[?&]' + p + '=([^&]*)').exec(window.location.search);
    return match && decodeURIComponent(match[1].replace(/+/g, ' '));
}
var gclid = getParam('gclid');
if(gclid){
    var gclsrc=getParam('gclsrc');
    if(!gclsrc || gclsrc.indexOf('aw') !== -1){
        setCookie('gclid', gclid, 90);
    }
}
</script>

 

However, the cookie will not generate, no matter what I do. I've checked the Javascript console in Chrome, and I'm getting this error:

 

Uncaught SyntaxError: Invalid regular expression: /+/: Nothing to repeat

 

getParam @ ?gclid=CIDc76Hx9sYCFZGBfgodpF0Bew:433

(anonymous function) @ ?gclid=CIDc76Hx9sYCFZGBfgodpF0Bew:435

 

Here's the page in question, it's just a test page:

 

http://www.cedrsolutions.com/sandbox/kylestestingpage/?gclid=CIDc76Hx9sYCFZGBfgodpF0Bew

 

Does anyone have any ideas?  Google's documentation implied this was going to go a LOT smoother than this.

1 ACCEPTED SOLUTION

Accepted Solutions
Marked as Best Answer.
Solution
Accepted by topic author Kyle L
September 2015

Re: Adwords GCLID tracking - Cookie won't set

Visitor ✭ ✭ ✭
# 9
Visitor ✭ ✭ ✭
Thanks! You're 100% correct, it was SOMETHING off with another plugin on the site. However, instead of going through all of the steps you mentioned, I just set up the script as a custom HTML tag in GTM, and everything works great now. Thanks again though Smiley Happy

View solution in original post

Re: Adwords GCLID tracking - Cookie won't set

Participant ✭ ✭ ✭
# 2
Participant ✭ ✭ ✭
Hi Kyle, how are things?

I didn't fully understand your question, but Google "GCLID" parameters is automatically appended to your URL once you link your Adwords account with your Analytics accout. You can check how, here:
https://support.google.com/adwords/answer/1704341?hl=en

Can you share more information on why do you need to store gclid inside a cookie?

Cheers.

Leandro Martinez | Basta1Click

Re: Adwords GCLID tracking - Cookie won't set

Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭
Yep, I definitely understand that. So I'm trying to go through this support page:
https://support.google.com/adwords/answer/2998031?hl=en

And I'm following Google's instructions, but the cookie itself doesn't generate. I'm stuck at that point until I can figure out why the cookie won't generate based off Google's code.

Re: Adwords GCLID tracking - Cookie won't set

Participant ✭ ✭ ✭
# 4
Participant ✭ ✭ ✭
Hi Kyle,

I ran a test just about now with the same code from the help article, but I couldn't check your URL to test it out. It's giving 404 error. Can you check and upload again? It worked here.

Leandro Martinez | Basta1Click

Re: Adwords GCLID tracking - Cookie won't set

Visitor ✭ ✭ ✭
# 5
Visitor ✭ ✭ ✭
Sorry! I changed the URL text but not the actual URL I guess

http://www.cedrsolutions.com/sandbox/kylestestingpage/?gclid=CIDc76Hx9sYCFZGBfgodpF0Bew

That link will work, sorry. I took out the dashes in case they were causing issues with the Regex component.

Re: Adwords GCLID tracking - Cookie won't set

Participant ✭ ✭ ✭
# 6
Participant ✭ ✭ ✭
I couldn't find the code on this page. Can you check?

Leandro Martinez | Basta1Click

Re: Adwords GCLID tracking - Cookie won't set

Visitor ✭ ✭ ✭
# 7
Visitor ✭ ✭ ✭
I'm so sorry, I must have forgotten to re-input it after my last troubleshooting step. it's there now, still behaving the same way though (throwing an error instead of generating a cookie)

Re: Adwords GCLID tracking - Cookie won't set

Participant ✭ ✭ ✭
# 8
Participant ✭ ✭ ✭

Hi Kyle,

 

Since I made some tests with the same code from the article help and it worked for me, I can only assume that the problem is on your page.

 

Notice that the code is NOT directly before the </body> tag no the HTML and that there're some codes after your Gclid Cookie, and maybe they're conflicting. This is my assumption (code from your template conflicting with google code).

 

Use the code below as a test. You could open a notepad, paste it and save as something.html. Then you'd upload to your website FTP and access it with the console opened

 

1st try: mysite.com.br/something.html (check for errors)

2nd try: mysite.com.br/something.html?gclid=anythingHere (check if it works)

 

IF this work, there's definitely some conflict going on, and then I should take a look at CHILD-THEMEs on Wordpress, to append the Gclid cookie directly before the </body> to test it out.

 

Here is the code:

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title of the document</title>
</head>

<body>
Content of the document......
<script type="text/javascript">
function setCookie(name, value, days){
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
var expires = "; expires=" + date.toGMTString();
document.cookie = name + "=" + value + expires;
}
function getParam(p){
var match = RegExp('[?&]' + p + '=([^&]*)').exec(window.location.search);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
var gclid = getParam('gclid');
if(gclid){
var gclsrc=getParam('gclsrc');
if(!gclsrc || gclsrc.indexOf('aw') !== -1){
setCookie('gclid', gclid, 90);
}
}
</script>
</body>

</html>


Leandro Martinez | Basta1Click

Marked as Best Answer.
Solution
Accepted by topic author Kyle L
September 2015

Re: Adwords GCLID tracking - Cookie won't set

Visitor ✭ ✭ ✭
# 9
Visitor ✭ ✭ ✭
Thanks! You're 100% correct, it was SOMETHING off with another plugin on the site. However, instead of going through all of the steps you mentioned, I just set up the script as a custom HTML tag in GTM, and everything works great now. Thanks again though Smiley Happy

Adwords GCLID tracking - Cookie won&#39;t set

Visitor ✭ ✭ ✭
# 10
Visitor ✭ ✭ ✭

Hi Kyle, I know it's been a while, but how did you complete your GTM set up? Do you have to define the gclid as a custom variable for the insertion code to work? I'm familiar enough with GTM to be dangerous, but still learning the ropes.