Created
August 22, 2016 11:19
-
-
Save OrenBochman/1b5a809db3eb89f8892e826dfbd6c858 to your computer and use it in GitHub Desktop.
grm_cleanUrlTracker
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
/** | |
* Cleans the URL based on the preferences set in the configuration options. | |
* set config queries from {{url_cleaner_conf}} | |
* custom dim storage not implemented - since it is more easily stored using a dataleyer var | |
* to setup combine set page path in with gtm tags | |
*/ | |
function() { | |
var opts = { 'stripQuery' : true, 'queryDimensionIndex': null, 'indexFilename' : 'index.html', 'trailingSlash': 'remove'}; | |
if( {{url_cleaner_conf}} != null) | |
opts = JSON.parse( {{url_cleaner_conf}} ); | |
var location = window.get('location'); | |
var page = window.get('page'); | |
var url = parseUrl(page || location); | |
var oldPath = url.pathname; | |
var newPath = oldPath; | |
// If an index filename was provided, remove it if it appears at the end of the URL. | |
if (opts.indexFilename) { | |
var parts = newPath.split('/'); | |
if (opts.indexFilename == parts[parts.length - 1]) { | |
parts[parts.length - 1] = ''; | |
newPath = parts.join('/'); | |
} | |
} | |
// Ensure the URL ends with or doesn't end with slash based on the | |
// `trailingSlash` option. Note that filename URLs should never contain | |
// a trailing slash. | |
if (opts.trailingSlash == 'remove') { | |
newPath = newPath.replace(/\/+$/, ''); | |
} | |
else if (opts.trailingSlash == 'add') { | |
var isFilename = /\.\w+$/.test(newPath); | |
if (!isFilename && newPath.substr(-1) != '/') { | |
newPath = newPath + '/'; | |
} | |
} | |
return ( newPath + (!this.opts.stripQuery ? url.search : '')); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment