Search code examples
google-analyticsseoanalyticsduplicates

Using Google Analytics how can I track different audience segments viewing the same page?


I'm looking for some general advice. The site I'm currently working on is full of duplicate content that's about to be deduplicated. But it was built that way to track different audiences visiting the pages by reporting on the URL hits.

Current Links

  • www.MySite.com/homeowner/painting
  • www.MySite.com/professional/painting
  • www.MySite.com/designer/painting

My concern is that at the end of the day, the person managing the analytics wants to be able to look at their report and say "We had X number of professionals visit the site." Simply deduping will elimate that.

I'm thinking Google Analytics might have a way to pass audience/tags in via the URL like this:

Example Links with Tracking

  • www.MySite.com/painting?tag=homeowner
  • www.MySite.com/painting?tag=professional
  • www.MySite.com/painting?tag=designer

Is this possible with Google Analytics? Does anyone have an example website using this?

I've looked into Custom Dimensions and Metrics but they seem to be overkill https://developers.google.com/analytics/devguides/collection/analyticsjs/custom-dims-mets


Solution

  • Custom Dimensions are not overkill, it's a reasonable idea for you to use them (because segmentation is what they are for, really).

    Using a url tag has a couple of disadvantages. For one The tagged pages will appear as three distinct rows in your reports - you will be unable to get an aggregated number for www.MySite.com/painting, instead you will have three Urls (or as many as you have parameters).

    Secondly, homeowner etc. are attributes that belong to a session, or even a user (if the role cannot change from visit to visit). However if you track them via url parameters they have only a hit level scope, i.e. they are recorded as a property of the viewed page, not the viewing visitor. If you record this as a session scoped variabe you need to set it only at the first pageview, and the value will be applied to all subsequent pageviews for that session.

    So an easy way (example assumes you are using php) might be to use

    if(isset($_GET['tag']) {
        ga('send', 'pageview', {
          'dimension1':  "<?php echo filter_input(INPUT_GET, 'tag', FILTER_SANITIZE_ENCODED); ?>"
        }); 
    } else {
       ga('send', 'pageview'); 
    }
    

    in your tracking code after you have created a session scoped custom dimension in your property settings ("dimension1" referring to the first custom dimension in your account, the numeric index changes for each dimension. The dimension name is only used in the reports, not the tracking code). You need to be careful not to send an empty value when the query string is not present - a session scoped custom dimension only records the last value from a session, if you send empty values you overwrite the value you recorded at the first pageview.

    Alternatively you can do this without changing the tracking code at all - create a custom advanced filter to capture the value from the query string, a second to copy the value to your custom dimension and a third to remove the query string from the url. However while that's a neat trick using code is much easier.