here is a basic solution using ES6
to use simply import ParseUrlUtils from './parse_url_utils'
and then add this file:
const ParseUrlUtils = {
getParam: (name) => {
if(name=(new RegExp('[?&]'+encodeURIComponent(name)+'=([^&]*)')).exec(window.location.search))
return decodeURIComponent(name[1]);
},
UTMPassThroughString: () => {
const default_utms = ['utm_campaign', 'utm_term',
'utm_source', 'utm_medium', 'utm_content']
var utm_keys = []
var utm_string
default_utms.map((utm_key) => {
utm_keys.push({key: utm_key, value: ParseUrlUtils.getParam(utm_key)})
})
utm_keys = utm_keys.filter((obj) => obj.value !== undefined)
if (utm_keys.length > 0) {
utm_string = "?" + utm_keys.map((ele) => {
return ele.key + "=" + ele.value
}).join("&")
}
return utm_string
}
}
export default ParseUrlUtils