- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Subscribe
- Printer Friendly Page
Adwords GCLID tracking - Cookie won't set
[ Edited ]July 2015 - last edited July 2015
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Email to a Friend
- Report Abuse
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.
Re: Adwords GCLID tracking - Cookie won't set
July 2015
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Email to a Friend
- Report Abuse
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
July 2015
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Email to a Friend
- Report Abuse
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
July 2015
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Email to a Friend
- Report Abuse
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
July 2015
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Email to a Friend
- Report Abuse
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
July 2015
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Email to a Friend
- Report Abuse
Leandro Martinez | Basta1Click
Re: Adwords GCLID tracking - Cookie won't set
July 2015
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Email to a Friend
- Report Abuse
Re: Adwords GCLID tracking - Cookie won't set
July 2015
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Email to a Friend
- Report Abuse
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
Re: Adwords GCLID tracking - Cookie won't set
July 2015
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Email to a Friend
- Report Abuse

Adwords GCLID tracking - Cookie won't set
November
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Email to a Friend
- Report Abuse
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.