13

We are planning to use Google Analytics in our organization and I am in charge for setting it up. I was wondering how to deal with multiple environments. We sure do not want to collect data during development and QA (or maybe collect data to a different analytics account), but we want to when the site goes to production (obviously).

  • How do you deal with multiple environments and Google Analytics ?
  • Do you setup multiple accounts for Google Analytics and use either one depending on the environement ?

We're using ASP.NET 2.0, if that matters.

marco-fiset
  • 8,721
  • 9
  • 35
  • 46

4 Answers4

12

Another option would be Google Analytics profiles. Use the same account in all environments, but add a hostname filter in each profile to only include traffic from the respective environment.

For example, if your internal QA environment is qa.example.com, create a "QA" profile in Google Analytics with a custom filter to only include hostnames matching ^qa\.example\.com$. In your "Production" profile, do the opposite and exclude traffic from internal hostnames.

Matthew
  • 221
  • 2
  • 3
  • This is pretty easy to do, powerful, and one less variable to configure inside the config file. –  Aug 23 '16 at 19:33
9

The easiest answer is yes, setup multiple accounts for each environment. Then, replace the profile ID (the text that looks like 'UA-XXXXX-X', as seen here) with the correct ID from the desired environment.

Since you are using ASP.NET, you can store the profile ID in the web.config. This will lead to having to embed .NET code within JavaScript (which may or may not be okay). You could also configure the production configuration to be the only configuration to output the tracking code, but that would lead to code being released that wasn't tested.

Depending on how you host your site (dev.domain.tld, test.domain.tld, www.domain.tld) you can possibly use a single profile and then filter the results based on subdomain/folder/url. Google has a pretty good article on the various tracking scenarios.

Fammy
  • 416
  • 3
  • 8
1

Here is what I finally did (I did not like the idea of embedding .Net code inside javascript) :

  • I setup two Google Analytics account, one for development/testing and one for production
  • Create a page which returns the Google Analytics account id (from the web.config) as its body.
  • On $(document).ready, make an ajax call to the page
  • On Ajax complete, call the google analytics script with the response (which contains the account id)

So depending on the environment I deploy to, I use either version of the web.config file containing the right account id.

It's a bit more complicated, but it does not involve generating javascript from .NET, and it's been very simple with jQuery.

Here is the final code :

var setupGoogleAnalytics = function (clientNumber) {
    var gaq = gaq || [];
    gaq.push(['_setAccount', clientNumber]);
    gaq.push(['_setDomainName', 'none']);
    gaq.push(['_trackPageview']);

    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
}

$(document).ready(function(){
    $.ajax({
        url: "GetGAClientNumber.aspx",
        success: function(data) {
            setupGoogleAnalytics(data);
        }
    });
});

And it works perfectly !

marco-fiset
  • 8,721
  • 9
  • 35
  • 46
  • 8
    Seems wasteful to make an AJAX request on every page load simply to get the account number. – Bob Banks Nov 27 '13 at 19:59
  • 3
    There is nothing wrong with embedding an `<%= ASP %>` tag inside Javascript - just put the minimum inside the JS, and any other querying/logic elsewhere. – Simon East Nov 10 '14 at 04:53
0

Can you make sure that during development and QA, the site is only accessed internally from machines in your company network?

If yes, you could just use the same analytics account that will be used for production, and filter out all traffic from within your organization.

Quote from the link:

If you want to exclude internal traffic from appearing in your reports, you can filter out a specific IP address or a range of IP addresses. You can also use cookies to filter out visits from particular users. We'll explain how below.

Christian Specht
  • 1,863
  • 3
  • 15
  • 23
  • Although this definitely works, when wanting to test implementation of events and other real-time track-able data; it makes it all very hard to do without an isolated profile or view or use it under. –  Aug 23 '16 at 19:34