Last active
December 28, 2015 22:39
-
-
Save bigsweater/7572951 to your computer and use it in GitHub Desktop.
Lead tracking with jquery.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* This code relies on two small jQuery plugins: | |
* | |
* jQuery Cookie Plugin | |
* https://github.com/carhartl/jquery-cookie | |
* | |
* and | |
* | |
* jQuery replaceText - v1.1 - 11/21/2009 | |
* http://benalman.com/projects/jquery-replacetext-plugin/ | |
* | |
* | |
* Here's the script: | |
*/ | |
// getUrlVar function | |
// Courtesy http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html | |
$.extend({ | |
getUrlVars: function(){ | |
var vars = [], hash; | |
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'); | |
for(var i = 0; i < hashes.length; i++) | |
{ | |
hash = hashes[i].split('='); | |
vars.push(hash[0]); | |
vars[hash[0]] = hash[1]; | |
} | |
return vars; | |
}, | |
getUrlVar: function(name){ | |
return $.getUrlVars()[name]; | |
} | |
}); | |
// jQuery lead-tracking | |
var leadSrc = 'direct'; // Assume it's a direct visit | |
var referrer = document.referrer; | |
if ( $.getUrlVar('src') ) { // if there's a query variable called 'src' | |
leadSrc = $.getUrlVar('src'); // Then the lead source is the query variable. | |
} else { | |
if( referrer ) { // Otherwise, if there's a HTTP referer, | |
if ( ( referrer.indexOf( "google.com" ) !== -1 ) || ( referrer.indexOf( "search.yahoo.com" ) !== -1 ) || ( referrer.indexOf( "bing.com" ) !== -1 ) ) { // And that referer is a search engine | |
leadSrc = 'search'; // Set leadSrc to 'search' | |
} else { // If it's not one of the big three... | |
leadSrc = 'web'; // Then the lead source is somewhere on the Web | |
} | |
} | |
} | |
// If it doesn't already exist, store a cookie with the leadSrc as its content, | |
// and set leadSrc to the value of the cookie. | |
if ( !$.cookie( 'cr_leadSrc' ) ) { | |
$.cookie( 'cr_leadSrc', leadSrc, { path: '/', expires:999 } ); | |
} | |
leadSrc = $.cookie( 'cr_leadSrc' ); | |
// Create a function that swaps content out. | |
var swapper = function(oldContent, newContent) { | |
if (typeof oldContent === 'object') { | |
$.each(oldContent, function(i){ | |
$('body *').replaceText(this, newContent[i]); | |
}); | |
} else { | |
$('body *').replaceText( oldContent, newContent ); | |
} | |
}; | |
var oldContent = [ // An array, in case there are multiple numbers to change. Can also be a string | |
/\(?\b[4][0][4]\)?[-. ]?[2][3][3][-. ]?[2][2][3][0]\b/g // A regular expression, in case the format of the number varies from page to page | |
]; | |
var newContent = []; // Initialize the new variable | |
if ( leadSrc === 'adwords' ) { // In this instance, the '?src=' query variable is 'adwords', but it could be anything. | |
newContent = [ | |
'(888) 225-9471' | |
]; | |
swapper(oldContent, newContent); | |
} | |
// Swapping out subject lines is just a matter of using the jQuery .val function, | |
// e.g. $('.yoursubjectlineinput').val('New subject line') | |
// called at the same time as the 'swapper' function. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment