2
votes

I am trying to filter Google Analytics data for my company site based on a cookie. I don't want to track internal traffic, but I can't just filter based on an IP address range because there are some internal users who we want to still track. I have some pretty simple code for adding a cookie, but I am just not sure where to add the code. I am really new to cookies, and couldn't find anything online that was clear on how to actually add or use the cookie.

<html>
<head>

<title>Remove My Internal Traffic from Google Analytics</title>

<script type="text/javascript">
          var _gaq = _gaq || [];
          _gaq.push(['_setAccount', 'UA-XXXXX-YY']);
          _gaq.push(['_setVar','employee']);
          _gaq.push(['_trackPageview']);
          (function() {
            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);
          })();

So my question is, where does this code actually go? Thanks for helping out my novice skills with cookies.

1
Don't fiddle around. Have you sysadmin deploy the opt out-Plugin companywide (tools.google.com/dlpage/gaoptout) if you don't want to track employees (can be disabled in the browser settings). - Eike Pierstorff
I am aware of the plugin. It is definitely an easy solution, but I am trying to build my knowledge of cookies. Any help on the question is appreciated. - Landon

1 Answers

3
votes

Do not use setVar (this is deprecated), use _setCustomVar:

_setCustomVar(index, name, value, opt_scope)

The call goes before the _trackPageview Call.

There are five custom vars in standard GA (50 in premium), that's "index". 'Name' and 'value' should be clear.

CustomVars are either valid for the current page, for the session or for the visitor (in the last case they are valid until the visitors clears the cookies in his browsers unless he waits six months before he visits you site again).

Like every instruction with the asynonchronous GA code this is "pushed" on the gaq-Array, so the correct call would be:

 _gaq.push(['_setCustomVar',
      1,                   // This custom var is set to slot #1.  Required parameter.
      'Items Removed',     // The name acts as a kind of category for the user activity.  Required parameter.
      'Yes',               // This value of the custom variable.  Required parameter.
      2                    // Sets the scope to session-level.  Optional parameter.
   ]);

which is taken from the Google documentation here: https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingCustomVariables#setup.

I still maintain that for your use case the opt-out plugin is the better solution.

UPDATE: Thinking about it I don't think you need setCustomVar or custom cookies at all. Have your employees go to your website via a link like:

mywebsite.com?utm_source=allyourbasearebelongtous

Then go to the profile settings and create a custom filter, set to exclude, filter field "campaign source" , filter pattern "allyourbasearebelongtous" (or whatever name you gave to your campaign parameter).

This uses also a cookie (the standard google cookie) but does not need any custom code at all. The campaign source parameter is valid until they visit another campaign geared towards your site, so if somebody wants to test the GA code they need to delete their cookies or use incognito mode (but that't not different from setting a custom cookie or setCustomVar-methods).