Analytics Log - Adil Khan

View Original

How to extract query parameters in Google Tag Manager

Say, you have a page on your site that looks like this:

http://www.example.com/searchfilter.aspx?field1=XXX&field2=YYY&field3=ZZZ&hf1=XXX&hf2=YYY&hf3=ZZZ

This page is a based on a search filter and you'd want to keep the results separate from site search query [which you can set at the view level in Google Analytics].

How to extract query parameters from the URL in Google Tag Manager

You want to extract field1, field2 and field3 values from the query parameter portion of the link and pass them as events to Google Analytics.

1. Create 3 separate variables for each field. This can be done by choosing the URL type in variable config + choosing 'query' as the component and finally, the field name [field1 in the screenshot below].

2. Create your triggers [3 separate ones where {{field 1}} does not equal undefined, {{field 2}} does not equal undefined and {{field 3}} does not equal undefined. The triggers have been kept separate as you might have searchresults pages where a user only chooses some of the search filter options and not all. If all three fields are included in the same trigger then it would be an AND condition [ field 1 + field 2 + field 3 should not equal undefined].

3. Setup your GA event where Category could be along the lines of 'Search filter results' while Action would be {{field 1}} / {{field 2}} / {{field 3}}.

Once data starts coming in and you want to separate out the fields in Excel, you can use 'Text to column' feature > Delimeter > Use '/' as the symbol that separates the field and that's it. You'd now have the different combinations used by people on your site.

Link to GTM Forum discussion and Simo's solution: https://productforums.google.com/forum/#!topic/tag-manager/ysNVK2J6F5g;context-place=mydiscussions