Skip to content

Instantly share code, notes, and snippets.

@maestrojed
Created September 9, 2020 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maestrojed/c21009654e7115d46b2cd7c6398c8fc1 to your computer and use it in GitHub Desktop.
Save maestrojed/c21009654e7115d46b2cd7c6398c8fc1 to your computer and use it in GitHub Desktop.
// Default to an empty string
var source = "";
var medium = "";
var campaign = "";
// Look for and use UTM Source, Medium, and Campaign parameters
var sourceParam = new RegExp('[\?&]utm_source=([^&#]*)').exec(window.location.href);
if (sourceParam !== null){
source = decodeURI(sourceParam[1]) || 0;
}
var mediumParam = new RegExp('[\?&]utm_medium=([^&#]*)').exec(window.location.href);
if (mediumParam !== null){
medium = decodeURI(mediumParam[1]) || 0;
}
var campaignParam = new RegExp('[\?&]utm_campaign=([^&#]*)').exec(window.location.href);
if (campaignParam !== null){
campaign = decodeURI(campaignParam[1]) || 0;
}
// Look for and use Linkwell Source, Medium, and Campaign parameters. Override any UTM data
sourceParam = new RegExp('[\?&]source=([^&#]*)').exec(window.location.href);
if (sourceParam !== null){
source = decodeURI(sourceParam[1]) || 0;
}
mediumParam = new RegExp('[\?&]medium=([^&#]*)').exec(window.location.href);
if (mediumParam !== null){
medium = decodeURI(mediumParam[1]) || 0;
}
campaignParam = new RegExp('[\?&]campaign=([^&#]*)').exec(window.location.href);
if (campaignParam !== null){
campaign = decodeURI(campaignParam[1]) || 0;
}
ga('set', 'campaignSource', source);
ga('set', 'campaignMedium', medium);
ga('set', 'campaignName', campaign);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment