Expand my Community achievements bar.

Check out the 3rd Edition of the AEP Community Lens for all the latest releases, resources, and Community updates
SOLVED

Getting multiple query parameters in the s.util.getQueryparam

Avatar

Level 3

I want to be able to capture the multiple query parameters in s.campaign variable.

URL

www.example.com?cmpid=hhhh&source=ssss&medium=dddd

Code

If(!s.campaign) s.campaign=s.Util.getQueryParam('cmpid')

When I adjust the code to do the following:

If(!s.campaign)

s.campaign=s.Util.getQueryParam('cmpid')

s.campaign=s.Util.getQueryParam('source')

Result =ssss - only the source value from the URL.

How do I modify my code to capture s.campaign =hhhh:ssss:dddd

1 Accepted Solution

Avatar

Correct answer by
Level 2

I belive you are overridding.

Try this - var ss = s.Util.getQueryParam('x')+":"+s.Util.getQueryParam('a')+":"+s.Util.getQueryParam('b');alert(ss);

0 Replies

Avatar

Correct answer by
Level 2

I belive you are overridding.

Try this - var ss = s.Util.getQueryParam('x')+":"+s.Util.getQueryParam('a')+":"+s.Util.getQueryParam('b');alert(ss);

Avatar

Level 3

Never mind. It worked!!

I used this:

s.campaign = s.Util.getQueryParam('cmpid')+":"+s.Util.getQueryParam('source')+":"+s.Util.getQueryParam ('medium');

Avatar

Level 3

I set this up in my main library management code.

But for any page (without Query parameters), s.campaign value is now showing ":::". How do I modify the code to not capture any value when the parameters are not present?

Avatar

Community Advisor

s.campaign  = [s.Util.getQueryParam('cmpid'),s.Util.getQueryParam('source'),s.Util.getQueryParam ('medium')].join(":").replace(/::/,'');

Avatar

Level 3

The same logic is not working for the below:

s.campaign=[s.Util.getQueryParam('source'),s.Util.getQueryParam('cmpid')].join("-").replace(/-/,'');

the value is being generated as sourcecmpid (without the dash). Any suggestions on modifying it?

Avatar

Community Advisor

I think you might be missing a couple foundational concepts.  Let's tear this thing apart and look at each part.

s.campaign  = [s.Util.getQueryParam('cmpid'),s.Util.getQueryParam('source'),s.Util.getQueryParam ('medium')].join(":").replace(/::/,'');

The first thing to understand is that there is a pipeline of activity, meaning that one operation feeds the one to the right which in turn feeds the next. Below I break it apart into three different lines of code:

s.campaign  = [s.Util.getQueryParam('cmpid'),s.Util.getQueryParam('source'),s.Util.getQueryParam ('medium')];

s.campaign = s.campaign.join(":");

s.campaign = s.campaign.replace(/::/,'');

Let's look at each line:

// Line 1 - This line creates a javascript Array from the values returned by three function calls.

s.campaign  = [s.Util.getQueryParam('cmpid'),s.Util.getQueryParam('source'),s.Util.getQueryParam ('medium')];

Let's decompose this line even more:

Now let's see it in action:

If our URL is http://www.foo.com/some/path?source=Apples&medium=Oranges&cmpid=Bananas

If we ran just the code above, we'd end up with the array, ["Bananas", "Apples", "Oranges"]

If our URL is http://www.foo.com/some/path?cmpid=Bananas

If we ran just the code above, we'd end up with the array, ["Bananas", "", ""]

If our URL is http://www.foo.com/some/path?name=Martha&age=32

If we ran just the code above, we'd end up with the array, ["", "", ""]

Two notes on the above:  First, note that the order of the parameters in the URL's query string does not matter. They are put into the array in a set order.  Second, note that if a query parameter does not exist, we put an empty string into the array.

Now for line 2:

This takes the array from the prior step and concatenates all of its parts into a string using the delimiter that we passed in.

Here are the examples from above:

Finally, line 3:

This line looks for specific patterns in a string and replaces that pattern with an empty string.   Essentially, find all instances of "::" and remove them.

Here are the examples from above:

Q.E.D

Now I leave it to you.  Armed with this new understanding can you answer your own question?

If not, DM me and I will give you the solution.

-Stew

Avatar

Level 3

Hi Stew,

Thank you so much for the great explanation! I really appreciate this to help me understand the fundamentals.

I think the code below for the URL first joins the query parameters with "-" and then applies the replace logic to show the value without the delimiter. Hence, I was seeing the values just sourcecmpid instead of source-cmpid.

s.campaign=[s.Util.getQueryParam('source'),s.Util.getQueryParam('cmpid')].join("-").replace(/-/,'');

However, with the code below, it will join the two parameters with the delimiter as source-cmpid and if the value is empty, it will return with just "-" for s.campaign:

s.campaign=[s.Util.getQueryParam('source'),s.Util.getQueryParam('cmpid')].join("-")

Now, I have to write a logic to replace "-" when there is no query parameter to return an empty string as below:

s.campaign=[s.Util.getQueryParam('source'),s.Util.getQueryParam('cmpid').join("-")].replace(/-/,'');

Did I get it right?

Avatar

Community Advisor

You make a good point. And, yes, you're on track.

I noticed a couple of things while thinking about your answer. Try the following replace logic.

["a","b"].join("-").replace(/(^-)|(-$)/g,'') --> "a-b"

["","b"].join("-").replace(/(^-)|(-$)/g,'') --> "b"

["a",""].join("-").replace(/(^-)|(-$)/g,'') --> "a"

["",""].join("-").replace(/(^-)|(-$)/g,'') --> ""

s.campaign=[s.Util.getQueryParam('source'),s.Util.getQueryParam('cmpid')].join("-").replace(/(^-)|(-$)/g,'');

Note that the solution changes slightly if you have 3 or more items in the array.

Avatar

Level 3

Thank you! This does work - and now I understand why the logic would differ based on the number of query parameters.

Appreciate your help!!