Tracking subdomains in Google Analytics

326 views Asked by At

I'm trying to track subdomains on my website in Google Analytics, but I don't want google to index the pages. I'm using the subdomains for offline marketing materials and would like to see the results in Google Analytics. For example, if someone goes to 123.example.com I'll setup a html redirect to redirect them to example.com, but I want it to show in Google Analytics that they came from 123.example.com.

I'm using the code below on an index.html page to prevent google or any search engine from indexing the page and then I would like it to redirect them to example.com.

<meta name="robots" content="noindex">
<meta http-equiv="refresh" content="0; url=http://example.com/" />

I'm not sure if this is the best way to do this and I've looked all over SO and online for the appropriate answer. I don't want the offline subdomains to be indexed, but I want them tracked in Google Analytics.

Do I need to modify my existing GA asynchronous code that I have on all pages for this?

1

There are 1 answers

0
Philip Walton On

If I were you I wouldn't do this client-side. And how you have it, it probably won't even work because the redirect is likely going to happen before the Google Analytics script can download and execute and then send the pageview to GA.

It would be pretty easy to send the hit to GA and redirect the user server-side. You can send hits to GA from the server using the Measurement Protocol.

If you absolutely must do this client-side, like I said, you're going to have to wait until the hit is sent to do the redirect. You could do something like this:

ga('send', 'pageview', {
  hitCallback: function() {
    window.location = 'http://example.com';
  }
});