AdWords
1.7K members online now
1.7K 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

need help grabbing gclid

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

hi all, i'm having trouble passing the gclid to my contact form.

 

i have enabled auto-tagging and added the following code to my template 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>
<script>
function readCookie(name) {
var n = name + "=";
var cookie = document.cookie.split(';');
for(var i=0;i < cookie.length;i++) {
var c = cookie[i];
while (c.charAt(0)==' '){c = c.substring(1,c.length);}
if (c.indexOf(n) == 0){return c.substring(n.length,c.length);}
}
return null;
}

window.onload = function() {
document.getElementById('gclid_field').value =
readCookie('gclid');
}
</script>

 

i have also added the following line to my contact form:

<input type="hidden" id="gclid_field" name="gclid_field" value="">

 

i can see in the console that there is a cookie set for gclid; however, the form submission is still showing up blank for the gclid variable. 

 

can anyone suggest what i am doing wrong?

2 Expert replyverified_user

Re: need help grabbing gclid

[ Edited ]
Top Contributor
# 2
Top Contributor

Hello, Ohad.

The first thing that comes to mind is that although you go through all the trouble to capture the gclid value (assuming that you captured it right), in the input form you're sending a blank value.

<input type="hidden" id="gclid_field" name="gclid_field" value=""> means you're sending "" instead of the gclid value. If you want to send it you need to write that line dynamically and make sure the value is the gclid value. Apart from that, the function reading the gclid value has to be executed before generating the dynamic line <input type="hidden" id="gclid_field" name="gclid_field" value="the_right_value">.

Hope it helps.

Calin Sandici, AdWords Top Contributor | Find me on: Google+ | Twitter | LinkedIn | myBlog
Was my response helpful? If yes, please mark it as the ‘Best Answer.’ Learn how here.

Re: need help grabbing gclid

Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭
ok, i split the javascript code and put the first script after the opening <body> tag and left the second script before the closing </body> tag.

sorry for asking a newb question, but is there an easy way to write the gclid value dynamically inside value=""?

Re: need help grabbing gclid

[ Edited ]
Top Contributor
# 4
Top Contributor

Since you're using Javascript I assume you want to write it client side, so it should be something like: 

<script>document.write ... etc.</script>
Calin Sandici, AdWords Top Contributor | Find me on: Google+ | Twitter | LinkedIn | myBlog
Was my response helpful? If yes, please mark it as the ‘Best Answer.’ Learn how here.

Re: need help grabbing gclid

Visitor ✭ ✭ ✭
# 5
Visitor ✭ ✭ ✭
ok, i'll try to figure out the code, thanx

Re: need help grabbing gclid

Top Contributor
# 6
Top Contributor
document.write("<input type=\"hidden\" id=\"gclid_field\" name=\"gclid_field\" value=\""+gclid+"\">");

This should probably do the trick, if my Javascript skills are still up to par.

Calin Sandici, AdWords Top Contributor | Find me on: Google+ | Twitter | LinkedIn | myBlog
Was my response helpful? If yes, please mark it as the ‘Best Answer.’ Learn how here.

Re: need help grabbing gclid

Visitor ✭ ✭ ✭
# 7
Visitor ✭ ✭ ✭
Did you consider tracking it on the server side (in your site’s code)? I do it that way. It works fine. It took 1 line of code to store it (Ruby on Rails).
Marked as Best Answer.
Solution
Accepted by topic author Ohad H
January 2016

Re: need help grabbing gclid

Visitor ✭ ✭ ✭
# 8
Visitor ✭ ✭ ✭
thanx, but i just received a suggestion to use:
<input type="hidden" id="gclid" name="gclid">
&lt;script&gt;document.getElementById("gclid").value=window.location.href.split("=")[1];&lt;/script&gt;

and it worked. thanx for your help!

Re: need help grabbing gclid

Top Contributor
# 9
Top Contributor
Hi Ohad H,

The real problem here is that you are trying to fill the input field from a cookie value. That cookie doesn't exist yet. You fill the value of the cookie with the gclid that comes from the query string, then you try to read that value and stuff it into the input field. That's just not going to work. Get the value of the gclid, store it to a local variable, then use that value to fill both the cookie and the input field and you should be good to go here.

Best of Luck!

Pete
petebardo -- Deadhead doing AdWords

Re: need help grabbing gclid

Visitor ✭ ✭ ✭
# 10
Visitor ✭ ✭ ✭
hmm, looks like i spoke too soon. the solution i mentioned works only if the user fills out the contact form on the page he landed. if he clicks through to another page, the gclid disappears from the URL and the form does not return a value for gclid. i would have thought google's code i noted in my original post above would retain the value, perhaps i am doing something wrong?