Remove Specific Parameters from URL

alim92960278

18-02-2016

Hi,

Our landing page URLs contain numerous parameters. I would like to populate a variable with a subset of these.

For example:
Original: www.example.com/home.html?st=nc&leadsource=events&product=generic&vendor=abc&vanity_url=example.com-...
Desired: www.example.com/home.html?leadsource=events&product=generic&vanity_url=example.com-ncevents

In this example, I have removed "st=nc" and "vendor=abc" as well as the "&"s.

Our real URLs can contain as much as 15 parameters and they're not always in the same order. I would also need to remove the associated "&" without removing the "?" if it's the first parameter.

Ali

----------------------------------------------------------------

After posting this I tried something that works but not well enough:

var OrigURL = "www.example.com/home.html?st=nc&leadsource=events&product=generic" var LSPos = OrigURL.indexOf('leadsource'); var LSPosEnd = OrigURL.indexOf('&',LSPos+1); var LSValue = OrigURL.substring(LSPos,LSPosEnd+1); var NewURL = OrigURL.replace(LSValue,""); _satellite.notify("OrigURL: "+OrigURL); _satellite.notify("LSPos: "+LSPos); _satellite.notify("LSPosEnd: "+LSPosEnd); _satellite.notify("LSValue: "+LSValue); _satellite.notify("NewURL: "+NewURL);

This works in removing the leadsource parameter but it has several problems

(1) If .indexof() returns -1 it will continue anyways

(2) It relies on the next "&" which may not exist if it's the last parameter

(3) I need a way to nest multiple of these and have the process continue to other parameters even if one of them is not found.

Ali

Accepted Solutions (1)

Accepted Solutions (1)

Amit_Kumar

MVP

19-02-2016

Ali Maleki wrote...

Thanks for the reply Amit.

Where would I use this? In a page load rule? Adobe Analytics Custom Page Code or 3rd party code?

Can you walk me through the main areas of this?

Ali

 

Adobe Analytics Custom Page Code

Answers (2)

Answers (2)

Amit_Kumar

MVP

18-02-2016

Question is that you want to remove certain variables from query string.. use the following sample code and see if this solves your problem.

I have not tested this so please fix the compiling errors if any.

 

var qParams; (window.onpopstate = function () { var match, pl     = /\+/g,  // Regex for replacing addition symbol with a space search = /([^&=]+)=?([^&]*)/g, decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); }, query  = window.location.search.substring(1); qParams = {}; while (match = search.exec(query)) qParams[decode(match[1])] = decode(match[2]); })(); // ?st=nc&leadsource=events&product=generic&vendor=abc&vanity_url=example.com-ncevents qParams = { st: "...", vendor: "..", leadsource: "...", product: "...", vanity_url: "..." } // delete all the params you want to delete delete qParams.st; delete qParams.vendor; //if native javascript then use serialize = function(obj, prefix) { var str = []; for(var p in obj) { if (obj.hasOwnProperty(p)) { var k = prefix ? prefix + "[" + p + "]" : p, v = obj[p]; str.push(typeof v == "object" ? serialize(v, k) : encodeURIComponent(k) + "=" + encodeURIComponent(v)); } } return str.join("&"); } //or from Jquery use: var str = jQuery.param( qParams );

alim92960278

19-02-2016

Thanks for the reply Amit.

Where would I use this? In a page load rule? Adobe Analytics Custom Page Code or 3rd party code?

Can you walk me through the main areas of this?

Ali