Skip to content

Instantly share code, notes, and snippets.

@mitchellhislop
Created July 26, 2011 21:59
Show Gist options
  • Save mitchellhislop/1108175 to your computer and use it in GitHub Desktop.
Save mitchellhislop/1108175 to your computer and use it in GitHub Desktop.
Sending the Search Referrer of an Action to GA
function getSearchString(){
var ref = top.document.referrer;
if (ref.indexOf('?') == -1) return;
var querys = ref.substr(ref.indexOf('?')+1);
var qsa = querys.split('&');
for (var i=0;i<qsa.length;i++) {
var qsip = qsa[i].split('=');
if (qsip.length == 1) continue;
if (qsip[0] == 'q' || qsip[0] == 'p') { // q= for Google + Bing, p= for Yahoo
var wordstring = unescape(qsip[1].replace(/\+/g,' '));
// console.log(wordstring); -- for debug mode
// TrackSearchSignup(wordstring); --production, pushes to Google Analytics
return true;
}
}
}
function TrackSearchSignup(wordstring){
_gaq.push(['_trackEvent', 'Signup', 'Register', 'referrer: ' + wordstring]);
return;
}
//<a href="blah.com" onClick="getSearchString();">Signup</a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment