Custom s.Util.getQueryParam: take into account fragment in URL - alcazes/Adobe-Analytics-from-A-To-Z GitHub Wiki

Plugin code

s.getQueryParamCustom = function(c, b, d, e, h) {
            console.log("custom");
            var f, g;
            e || (e = false);
            h || (h = s);
            b || (b = h.pageURL ? h.pageURL : window.location);
            d || (d = "&");
            return c && b && (b = "" + b, f = b.indexOf("?"), 0 <= f && (b = ( !e && "#"!= d && (/^.*(\?.*)#.*/.test(b)) ) ? (g = b.indexOf('#'), d + b.substring(f + 1, g) + d ) : (( e && "#"!= d && (/^.*(\?.*)#.*/.test(b))) ? (g = b.indexOf('#'), d + b.substring(g + 1) + d ) : d + b.substring(f + 1) + d ), f = b.indexOf(d + c + "="), 0 <= f && (b = b.substring(f + d.length + c.length + 1), f = b.indexOf(d), 0 <= f && (b = b.substring(0, f)), 0 < b.length))) ? h.unescape(b) : ""
        }
s.Util.getQueryParam = s.getQueryParamCustom;

Explanation of changes

Additional Parameters for function:

  • e : Specify if plugin needs to look for query param in query URL outside fragment or not
    • default : false -- the fragment part of the query URL is omitted
    • true : the plugin will only check if query param exist in fragment
    • false : the fragment part of the query URL is omitted. The plugin will only check if query param exist outside of fragment
  • h : specifies the adobe analytics object. Default to s if none specified.

Additional code:

  • e || (e = false); //check if fragment should be used or not, default to false.
  • h || (h = s); // this code allows to use the analytics object. Default to object s.
  • b || (b = h.pageURL ? h.pageURL : window.location); //replaced to use h as analytics object and window instead of w Added var g that will contain index of # to delimit fragment
  • Modified this code
0 <= f && (b = d + b.substring(f + 1) + d by 0 <= f && (b = ( !e && "#"!= d && (/^.*(\?.*)#.*/.test(b)) ) ? (g = b.indexOf('#'), d + b.substring(f + 1, g) + d ) : (( e && "#"!= d && (/^.*(\?.*)#.*/.test(b))) ? (g = b.indexOf('#'), d + b.substring(g + 1) + d ) : d + b.substring(f + 1) + d )
  • First condition check if e set to false, meaning that fragment should not be tested against. # needs to be present in url and # not set as delimiter
    • if true then get only query param between ? and #
    • if false then get only query params from fragment
  • h.unescape(b) //use analytics object specified

Use example

Example URL: http://localhost/Test%20Visitor%20ID/Double%20Visitor%20ID%20services/Double%20Visitor%20ID%20services.html?test=123&test2=asd#a=b;r=c

//Check if query param test2 exist outside fragment

Call : s.Util.getQueryParam("test2");

Outcome: asd

//Check if query param test2 exist inside fragment

Call: s.Util.getQueryParam("test2","","",true);

Outcome: ""

//Check if query param a exist inside fragment with default delimiter &

Call: s.Util.getQueryParam("a","","",true);

Outcome: b;r=c

//Check if query param a exist inside fragment with custom delimiter ;

Call: s.Util.getQueryParam("a","",";",true);

Outcome b;

//test if query param test2 exist in fragment with custom param #

Call : s.Util.getQueryParam("test2","","#",true);

Outcome: ""