analytics snippets

How to exclude your own traffic from Google Analytics reports and other JavaScript based analytics software

Tagged google, analytics, tracking, exclude, traffic  Languages ruby

Option 1: Changing your browser's user agent

Open the about:config page in Firefox by typing about:config in the address bar and pressing enter. Now change the general.useragent.extra.firefox setting to an easily identifiable string, for example the following:

Firefox/3.0 disable-tracking

Then in your code check that the user-agent string doesn't contain disable-tracking

<% if !request.user_agent.include?('disable-tracking') %>
<% end %>

Option 2:

Use one of Google Analytics native ways of excluding traffic from certain domains, IPs, user-agents or users having a specific browser cookie.

How to track user actions and custom events with Google Analytics and jQuery

Tagged jquery, google, analytics, track, click  Languages javascript

This is a customization of Rebecca Murphey's script:

$('a').each(function() {
    var $a = $(this);
    var href = $a.attr('href');
    if(typeof pageTracker == 'undefined') { return; }

    // Link is external
    if (href.match(/^http/) && !href.match(document.domain)) {
        $ {
            pageTracker._trackPageview('/external/' + href);
    } else {
        $ {
            pageTracker._trackPageview('/internal' + href);

Note that clicks are shown as page views in reports, so you should exclude them from all reports. A future version of Google Analytics will allow you to track events, such as mouse clicks, without affecting page view reporting, see this page on the new event tracking beta feature for more information.

Tracking 404 and 500 with Google Analytics

Tagged 404, 500, google, analytics, track  Languages javascript

New Google Analytics

_gaq.push(['_trackEvent', 'HTTP status', '404', '/xxx/what-a-fish']);

Old Google Analytics

Tracking 404 and 500 errors with Google Analytics is documented here, but I tend to forget so I'm putting the information here:

// 404
pageTracker._trackPageview("/404.html?page=" + document.location.pathname + + "&from=" + document.referrer);

// 500
pageTracker._trackPageview("/500.html?page=" + document.location.pathname + + "&from=" + document.referrer);


In Rails use the response status code to track any HTTP errors:

<% if response.status != 200 %>
_gaq.push(['_trackEvent', 'HTTP status', '<%= response.status %>', '<%= request.fullpath %>']);
<% end %>

How to use MySQL's ON DUPLICATE to track page views

Tagged mysql, duplicate, pageview, analytics  Languages sql

With MySQL 5 you can track page views with ON DUPLICATE.

The table:

create table page_views(
  uri varchar(500) not null,
  views int(11) not null default '0',
  primary key(uri)

The SQL:

insert into page_views (uri, views) values ('/products', 1) on duplicate key update views = views + 1;

See for more information.

Analyze Nginx's access logs with goaccess

Tagged access, analytics, goaccess, log  Languages bash

Show all statistics

sudo zcat /var/log/nginx/*.access.log.* | goaccess --log-format=COMBINED

Show server errors

sudo zcat /var/log/nginx/*.access.log.* | awk '$9~/500/' | goaccess --log-format=COMBINED