Reputation: 33
I have been using Google Analytics for a while. For one of my customers we use campaign tracking extensively. And recently we started collecting Multi Channel Funnel analytics. The primary reason for using it is so we can tracking which campaigns people are coming from and then when someone fills out an inquiry form, we have a goal set up (the inquiry thank you page), so Google Analytics can associate the goal with the campaign(s) that let that person to the thank you page.
My customer asked me today, “That information is great and extremely valuable. What would make it even better is if when the visitor arrived at the inquiry form, there was a way to extract the campaign information from the analytics cookie and put that into a hidden field on the form…so when the form is submitted, the sales person that receives it not only sees the information the visitor filled out, but they also have a field that shows which campaign(s) let that person to ultimately fill out the inquiry form.” That was not verbatim, but hopefully you understand what they are asking for. I know that the cookies must track all of the campaigns because Google Analytics shows that information in the reports. It’s automatically pulling out that information when someone lands on the inquiry form page and putting it into a hidden field (they don’t want the customer seeing that information) so the sales department can see it that will really complete the report system.
If anyone knows how to do this or has any insights that would be awesome.
Upvotes: 2
Views: 5588
Reputation: 401
Found this thread while debugging some legacy code (which may actually have copied/pasted this thread's existing answer!).
I thought I'd add some comments about a related problem that led me here, lest anyone else with the same problem end up here.
The code from @Yahel's answer worked fine as written, but when I rewrote it into a new context, it sometimes threw an error that killed all code calling that context in Chrome when the person didn't have any Google Analytics cookies on their computer.
I had transformed it into the following code:
function getGATS(){
var pairs = (/(?:^|; )__utmz=([^;]*)/.exec(document.cookie)||[]).slice(1).pop().split('.').slice(4).join('.').split('|');
var vals = {};
for (var i = 0; i < pairs.length; i++) {
var temp = pairs[i].split('=');
vals[temp[0]] = temp[1];
}
return {
'utm_source': (vals.utmgclid) ? "google" : vals.utmcsr,
'utm_medium': (vals.utmgclid) ? "cpc" : vals.utmcmd,
'utm_campaign': vals.utmccn,
'utm_content': vals.utmcct,
'utm_term': vals.utmctr
};
}
Another "getter" fuction then did this:
return $.extend({}, {
'is_constituent_checked' : $('#IsConstituent').is(':checked'),
'statuses' : getStatusesString()
}, getGATS());
That "getter" was called by an "addHiddenInputs()" function that looped through its return-value and added those inputs/values to the DOM. (This is for "onSubmit" form post-processing.)
=======
It turns out that when a user whose Chrome environment had no GA cookies ("alert(document.cookies);" didn't show any "utm" values) tried to submit my form, the entirety of that call to "addHiddenInputs()" failed to do its job.
This is because the first call to ".split()" on "(/(?:^|; )__utmz=([^;])/.exec(document.cookie)||[]).slice(1).pop()"* didn't work when that fragment evaluated to "undefined" (as is the case when there are no Google Analytics cookies).
Putting the body of the function inside of a "try{}" block and adding a "catch(err){...}" (in my case, with "return {};" as the "...") solved the problem.
Upvotes: 0
Reputation: 37315
Yes, it is possible.
var GATrafficSource = (function(){
var pairs = (/(?:^|; )__utmz=([^;]*)/.exec(document.cookie)||[]).slice(1).pop().split('.').slice(4).join('.').split('|');
var vals = {};
for (var i = 0; i < pairs.length; i++) {
var temp = pairs[i].split('=');
vals[temp[0]] = temp[1];
}
return {
'utm_source': (vals.utmgclid) ? "google" : vals.utmcsr,
'utm_medium': (vals.utmgclid) ? "cpc" : vals.utmcmd,
'utm_campaign': vals.utmccn,
'utm_content': vals.utmcct,
'utm_term': vals.utmctr
};
}());
You can then put whichever campaign variables you want into hidden fields.
For example, if you have:
<input type="hidden" name="utm_source" class="gacampaign" id="utm_source">
You could fill it like this:
document.getElementById("utm_source").value = GATrafficSource.utm_source;
Upvotes: 9