PaRsH
PaRsH

Reputation: 1320

Google analytics Site search is not working as expected

I just started using Google analytics for my website. I found that, enabling site search for a site is very easy as mentioned below,

enter image description here

But my site doesn't have the search term in the query string, instead it is part of the url as mentioned below,

https://mydomian.net/search#q=boxtest18

for some internal reason i cannot modify the code to append search term as querystring to url.

This article explained how to configure site search in GA, if Site Search Term Is Present in the URL: NOT As Query Parameter (Single Term).

When i tried, it doesn't seem working.

Below is my filter configuration.

enter image description here

What i'm doing wrong here?

Can i use the filter for this kind of url?

Is my regular expression correct?

Help me out here.

Upvotes: 1

Views: 2759

Answers (3)

PaRsH
PaRsH

Reputation: 1320

After digging into Google analytics and Google tag manager i found the solution. Solution was very easy in my case.

For every search, search term was updating in the url fragment. In the below case, i searched for boxtest18 and url updated like below.

https://mydomian.net/search#q=boxtest18

For this, i had crerated a Tag in Google tag manager and triggered it on history change event. That resovled my problem.

Here are the steps.

  1. In google tag manager i have added new tag and filled highlighted info. enter image description here

  2. Fragment value is a variable which i created. enter image description here

  3. For the new tag i have created the triggering event as shown below. enter image description here

And make sure you have the tracking code script and Google tag manager script in your web page. That's it. Hope it helps someone.

Upvotes: 0

h.maac
h.maac

Reputation: 56

As Michele mentioned you may append the hash when the query is visible in url hash;

gtag('config', 'GA_TRACKING_ID', {
  'page_path': location.pathname + location.hash
});

though if you are using requests (ajax or any restful api) you should manually trigger a page view as a virtual page.

  pagePath = location.pathname
  gtag('config', 'GA_TRACKING_ID', {page_path: pagePath + '?gender=' + request.gender });

  // or if using analytics.js
  ga('send', 'pageview', pagePath + '?gender=' + request.gender);

For more info, here is the help section for sending posts https://support.google.com/analytics/answer/1012264?hl=en#Post


Pageview Request Object

You may use these to manually trigger a page view for a virtual page. If you are looking to use analytics.js (ga()) please refer to this documentation

gtag('config', 'GA_MEASUREMENT_ID', {
  'page_title' : 'homepage',
  'page_path': '/home'
});

Parameter name  Value type  Required    Description
page_title      string      No          The page's title.
page_location   string      No          The page's URL.
page_path       string      No          The path portion of location. This value 
                                            must start with a slash (/) character.

For more info, here is the official documentation https://developers.google.com/analytics/devguides/collection/gtagjs/pages


Event Request Object

I am adding this section for completeness

You may use these to trigger a tracking event. If you are looking to use analytics.js (ga()) please refer to this documentation

gtag('event', <action>, {
  'event_category': <category>,
  'event_label': <label>,
  'value': <value>
});

action is the string that will appear as the event action in Google Analytics Event reports.

category is the string that will appear as the event category.

label (optional) is the string that will appear as the event label.

value (optional) is a non-negative integer that will appear as the event value.

For more info, here is the official documentation https://developers.google.com/analytics/devguides/collection/gtagjs/events

Upvotes: 1

Michele Pisani
Michele Pisani

Reputation: 14199

In Google Analytics the URL paths that are passed to GA with your Pageview hits are stripped of these fragments.

You can update the data before sending it to Analytics (track pageviews).

gtag('config', 'GA_TRACKING_ID', {
  'page_path': location.pathname + location.hash
});

Or you can use Google Tag Manager to get the fragment, more info here: https://www.simoahava.com/gtm-tips/track-url-fragments-as-pageviews/

Then you can handle it as you like or replace the # to the ? with a filter.

Upvotes: 0

Related Questions