Query string parameters in a browser URL frequently contain important data for Analytics. Use the Util.getQueryParam()
method to retrieve data from the query string.
You can get query string parameter data by setting values in data elements.
The query string parameter value is stored in the data element. You can then reference the data element in rules to assign the desired variables.
Call the s.Util.getQueryParam()
method to retrieve a query string value from the browser URL. The string argument containing a query string parameter is required. This method returns a string, which you can assign to Analytics variables:
s.eVar1 = s.Util.getQueryParam("cid");
A second optional argument allows you to specify the string to check for query string parameters. By default, the utility looks at the browser URL.
// Search a custom string for query string parameter
var customString = "https://example.com?q=search";
// eVar1 is set to "search"
s.eVar1 = s.Util.getQueryParam("q",customString);
A third optional argument allows you to customize the query string delimiter. Its default value is &
. You can change this value if your query string uses a different delimiter.
var customString = "https://example.com?q1=value1;q2=value2;q3=value3";
// eVar1 is set to "value2"
s.eVar1 = s.Util.getQueryParam("q2",customString,";");
A similar plug-in named s.getQueryParam
is available. This plug-in contains more advanced features, but is also more complex and not included in AppMeasurement by default.