Last active
January 17, 2019 17:39
-
-
Save robdimarco/a44288f06e85286b8aa9917ca832c6b3 to your computer and use it in GitHub Desktop.
Example instapage JS integration
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
/* | |
* Utility function to get the value of an HTTP parameter | |
* | |
* Common Usage: | |
* | |
* var utmTerm = getParameterByName('utm_term'); | |
* | |
* @param name - The name of the HTTP parameter you would like to get | |
* @param url [OPTIONAL] - The URL to parse to get the parameter from. If unspecified, | |
* it will get the URL of the current page. | |
* | |
* @returns if the parameter is found, it will return the parameter value. If not found returns null. | |
*/ | |
function getParameterByName(name, url) { | |
if (!url) url = window.location.href; | |
name = name.replace(/[\[\]]/g, "\\$&"); | |
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), | |
results = regex.exec(url); | |
if (!results) return null; | |
if (!results[2]) return ''; | |
return decodeURIComponent(results[2].replace(/\+/g, " ")); | |
}; | |
/* | |
* Oftentimes, we do not want to fire a tracking pixel if the user has checked no on a required question. | |
* @param form - The form to look for checked radio buttons in. | |
* @param value - The value of the checked radio button. If not specified, a default value of "No" is used. | |
* | |
* @returns true if a radio button is checked with the specified value, false if not. | |
*/ | |
function nonBillableRadioButtonChosen(form, value) { | |
value = typeof value !== 'undefined' ? value : 'No'; | |
return $(form).find('input[value="' + value + '"][type="radio"]:checked').length > 0 | |
} | |
/* | |
* Oftentimes, we do not want to fire a tracking pixel if the user has chosen a non-monetizable value from a select list. | |
* This function wit | |
* @param form - The form to look for checked radio buttons in. | |
* @param value - The value of the select option to look for. If not specified, a default value of "Other Injury" is used. | |
* | |
* @returns true if a option is selected with the specified value, false if not. | |
*/ | |
function nonBillableSelectOptionChosen(form, value) { | |
value = typeof value !== 'undefined' ? value : 'Other Injury'; | |
return $(form).find('option[value="' + value + '"]:selected').length > 0; | |
} | |
/* | |
* EXAMPLE utlity function for embedding tracking | |
*/ | |
function embedPixel(utmTerm) { | |
var iframe = document.createElement('iframe'); | |
var transId = 'ts' + new Date().getTime(); | |
iframe.style.display = "none"; | |
iframe.width = 1; | |
iframe.height = 1; | |
iframe.frameborder = 0; | |
iframe.src = "https://www.example.com/ipx.php?hid=" + utmTerm + '&sid=3961&transid=' + transId; | |
document.body.appendChild(iframe); | |
} | |
/* | |
* Example Instapage success function. It requires no non-billable selections to be chosen and a UTM Term to be present. | |
*/ | |
window.instapageFormSubmitSuccess = function( form ){ | |
if ( nonBillableRadioButtonChosen(form) || nonBillableSelectOptionChosen(form) ) { | |
// Do not fire pixel if Other | |
return; | |
} | |
var utmTerm = getParameterByName('utm_term'); | |
if (utmTerm === null) { | |
return; | |
} | |
embedPixel(utmTerm); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment