How to add query parameter before #stq= fragment

Hey there,

Great question, and thank you for pointing out that the GA tutorial could use a refresh for clarity. :slight_smile:

With your Google Analytics tracking code installed on your site and when using the WordPress plug-in or the standard JavaScript embed code snippet provided in the Dashboard, capturing and passing the query parameter to GA will function without the need for any additional intervention.

If you’re using our client libraries to implement your Site Search experience, you’ll need to pass along a virtual page view to Google Analytics. They’ve some additional documentation here:

Also, here’s a sample of some JavaScript that you can use as a starting point to parse, convert, and pass that parameter along properly:

var url = window.location.href;
var re = new RegExp("\#stq=(.+)");
var m = url.match(re);

if (m != null) {
    var track = "/search?stq=" + m[1];
    ga('send', 'pageview', track);
} else {
    ga('send', 'pageview');

For the above, modify the regex for the re variable as needed, as well as the root path structure for the track variable.

I hope this helps!

NOTE: The above methods and conditions do not apply to GA tracking that is being implemented via Google Tag Manager.