Skip to content

Instantly share code, notes, and snippets.

@baldawayash15
Last active November 9, 2022 16:36
Show Gist options
  • Save baldawayash15/de47e31cad3d76f748ea3fb5f20a2c36 to your computer and use it in GitHub Desktop.
Save baldawayash15/de47e31cad3d76f748ea3fb5f20a2c36 to your computer and use it in GitHub Desktop.
UTM Tracking for Website Referrer and URL Parameters
<!-- (Dependency) Add this before the below script !-->
<script src="https://unpkg.com/[email protected]/dist/url.min.js"></script>
<script>
// Extract UTM parameters from function
let utm_source = getParameterByName('utm_source');
let utm_medium = getParameterByName('utm_medium');
let utm_campaign = getParameterByName('utm_campaign');
let utm_content = getParameterByName('utm_content');
let utm_term = getParameterByName('utm_term');
let utm_ad_group = getParameterByName('utm_ad_group');
// Set Inital Variables
let utmSource = utm_source !== null ? utm_source : 'litify';
let utmMedium = utm_medium !== null ? utm_medium : 'direct';
let utmCampaign = utm_campaign !== null ? utm_campaign : 'not-provided';
let utmContent = utm_content !== null ? utm_content : 'not-provided';
let utmTerm = utm_term !== null ? utm_term : 'not-provided';
let utmAdGroup = utm_ad_group !== null ? utm_ad_group : 'not-provided';
// Parse Referrer Host Name
const domain_ref = String( URLJS( document.referrer, "host" ) );
const isRefURL = Boolean( URLJS.is_url( document.referrer ) );
const urlQuery = String( URLJS.parseUrl( window.location.href, "search" ) );
// Checking URL Referrer Regex
const checkdomainRegex = /(google|facebook|twitter|t|linkedin|bing|search\.yahoo|youtube|litify|litifydev)\.(com|co|local)/;
const checkdomainRegexRes = checkdomainRegex.test(domain_ref);
// Google Organic Search
if( domain_ref.includes("google.com") && urlQuery.length === 0 ) {
utmSource = 'google';
utmMedium = 'organic'
}
// Bing Organic Search
if( domain_ref.includes("bing.com") && urlQuery.length === 0 ) {
utmSource = 'bing';
utmMedium = 'organic'
}
// Yahoo Organic Search
if( domain_ref.includes("search.yahoo.com") && urlQuery.length === 0 ) {
utmSource = 'yahoo';
utmMedium = 'organic'
}
// Facebook Organic Social
if( domain_ref.includes("facebook.com") || domain_ref.includes("l.facebook.com") && urlQuery.length === 0 ) {
utmSource = 'facebook';
utmMedium = 'organicsocial'
}
// Twitter Organic Social
if( domain_ref.includes("twitter.com") || domain_ref.includes("t.co") && urlQuery.length === 0 ) {
utmSource = 'twitter';
utmMedium = 'organicsocial'
}
// LinkedIn Organic Social
if( domain_ref.includes("linkedin.com") || domain_ref.includes("lnkd.in") && urlQuery.length === 0 ) {
utmSource = 'linkedin';
utmMedium = 'organicsocial'
}
// Youtube Organic Social
if( domain_ref.includes("youtube.com") && urlQuery.length === 0 ) {
utmSource = 'youtube';
utmMedium = 'organicsocial'
}
// Referral
if(
checkdomainRegexRes === false &&
isRefURL
) {
utmSource = domain_ref;
utmMedium = 'referral';
}
// If domain referrer is Litify then change to direct
// if( domain_ref.includes('litify.com') ) {
// utmSource = 'litify';
// utmMedium = 'direct';
// }
// PaidSocial
// if(
// utm_medium === 'paidsocial' &&
// utm_source === 'facebook' ||
// utm_source === 'linkedin' ||
// utm_source === 'twitter' ||
// utm_source === 'youtube'
// ) {
// utmSource = utm_source;
// utmMedium = 'paidsocial'
// }
// PaidSearch
// if(
// utm_medium === 'paidsearch' &&
// utm_source === 'google' ||
// utm_source === 'bing' ||
// utm_source === 'yahoo'
// ) {
// utmSource = utm_source;
// utmMedium = 'paidsearch'
// }
// cpc
// if(
// utm_medium === 'cpc' &&
// utm_source === 'google' ||
// utm_source === 'bing' ||
// utm_source === 'yahoo'
// ) {
// utmSource = utm_source;
// utmMedium = 'cpc'
// }
// display
// if(
// utm_medium === 'display' &&
// utm_source === 'google' ||
// utm_source === 'bing' ||
// utm_source === 'yahoo'
// ) {
// utmSource = utm_source;
// utmMedium = 'display'
// }
// email
// if(
// utm_medium === 'email' &&
// utm_source === 'hubspot' ||
// utm_source === 'salesloft' ||
// utm_source === '3rd-party'
// ) {
// utmSource = utm_source;
// utmMedium = 'email'
// }
document.cookie = `utm_source=${utmSource}`;
document.cookie = `utm_medium=${utmMedium}`;
document.cookie = `utm_campaign=${utmCampaign}`;
document.cookie = `utm_content=${utmContent}`;
document.cookie = `utm_term=${utmTerm}`;
document.cookie = `utm_ad_group=${utmAdGroup}`;
function getParameterByName(name, url = window.location.href) {
name = name.replace(/[\[\]]/g, '\\$&');
let regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment