Skip to content

Instantly share code, notes, and snippets.

@parhumm

parhumm/virtualPagePath.js

Last active Dec 13, 2019
Embed
What would you like to do?
GTM virtualPagePath Variable
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