Created
September 14, 2020 23:14
-
-
Save luizlopescom/a93d085c8ded887b011783f1d0e57fce to your computer and use it in GitHub Desktop.
Get UTM Values via javascript
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
//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