Skip to content

Instantly share code, notes, and snippets.

@luizlopescom
Created September 14, 2020 23:14
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 luizlopescom/a93d085c8ded887b011783f1d0e57fce to your computer and use it in GitHub Desktop.
Save luizlopescom/a93d085c8ded887b011783f1d0e57fce to your computer and use it in GitHub Desktop.
Get UTM Values via javascript
//GET UTM VALUES
//get url
var urlQueryString = window.location.search;
//set params
var urlParams = new URLSearchParams(urlQueryString);
//set vars to empty to avoid receiving 'NULL' on forms
var utm_source = ' ';
var utm_medium = ' ';
var utm_campaign = ' ';
var utm_content = ' ';
//check if params exists
if(urlParams !== '') {
//if not null, set value
if(urlParams.get('utm_source') !== null) {
var utm_source = urlParams.get('utm_source');
}
if(urlParams.get('utm_medium') !== null) {
var utm_medium = urlParams.get('utm_medium');
}
if(urlParams.get('utm_campaign') !== null) {
var utm_campaign = urlParams.get('utm_campaign');
}
if(urlParams.get('utm_content') !== null) {
var utm_content = urlParams.get('utm_content');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment