Skip to content

Instantly share code, notes, and snippets.

@robdimarco
Last active January 11, 2019 18:57
Show Gist options
  • Save robdimarco/692867b1e459b2cc5e7f5850496b2212 to your computer and use it in GitHub Desktop.
Save robdimarco/692867b1e459b2cc5e7f5850496b2212 to your computer and use it in GitHub Desktop.
Useful functions for creating pixel firing
/*
* 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment