Last active
December 13, 2019 14:07
-
-
Save parhumm/833be9a8f829dad30c1a32e3ec711191 to your computer and use it in GitHub Desktop.
GTM virtualPagePath Variable
This file contains hidden or 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
function virtualPagePath() { | |
/** | |
* Get the URL parameters | |
* source: https://css-tricks.com/snippets/javascript/get-url-variables/ | |
* @param {String} url The URL | |
* @return {Object} The URL parameters | |
*/ | |
var getParams = function (url) { | |
var params = {}, | |
parser = document.createElement('a'); | |
parser.href = url; | |
var query = parser.search.substring(1), | |
vars = query.split('&'); | |
for (var i = 0; i < vars.length; i++) { | |
var pair = vars[i].split('='); | |
params[pair[0]] = decodeURIComponent(pair[1]); | |
} | |
return params; | |
}; | |
var pagePath = location.pathname, | |
queryParams = getParams(window.location.href), | |
queryParamsStr = ''; | |
queryParams = (queryParams ? queryParams : {}); | |
if (queryParams && Object.keys(queryParams).length) { | |
for (var key in queryParams) { | |
if (key.indexOf('utm_') === -1) { // Exclude UTM Parameters | |
queryParamsStr += 'QS'; | |
queryParamsStr += '/' + key; | |
queryParamsStr += '/' + queryParams[key]; | |
queryParamsStr += '/'; | |
} | |
} | |
if (key.length) { | |
if (pagePath && pagePath !== '' && pagePath[pagePath.length-1] !== '/') { | |
pagePath = pagePath + '/' | |
} | |
pagePath = pagePath + queryParamsStr; | |
} | |
} | |
return pagePath; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment