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

Apache posted 4 months ago by christian

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

The table:

   1  create table page_views(
   2    uri varchar(500) not null,
   3    views int(11) not null default '0',
   4    primary key(uri)
   5  )

The SQL:

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

See http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html for more information.

Tagged mysql, duplicate, pageview, analytics

How to track and store pageviews and unique visitors with MySQL

SQL posted 4 months ago by christian

Create a table:

   1  create table page_views (id VARCHAR(50), views INTEGER) primary key id;

Use the ON DUPLICATE declaration:

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

Idea from http://simonwillison.net/2009/Jun/30/analytics/

Tagged mysql, analytics, pageview, tracking

Tracking 404 and 500 with Google Analytics

JavaScript posted about 1 year ago by christian

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

   1  // 404
   2  pageTracker._trackPageview("/404.html?page=" + document.location.pathname + document.location.search + "&from=" + document.referrer);
   3  
   4  // 500
   5  pageTracker._trackPageview("/500.html?page=" + document.location.pathname + document.location.search + "&from=" + document.referrer);

In Rails I set the response code and use that instead of hardcoding it in the view:

   1  <% if response.status != 404 %>
   2  pageTracker._trackPageview();
   3  <% else %>
   4  pageTracker._trackPageview("/404.html?page=" + document.location.pathname + document.location.search + "&from=" + document.referrer);
   5  <% end %>

Tagged 404, 500, google, analytics, track

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

JavaScript posted about 1 year ago by christian

This is a customization of Rebecca Murphey’s script:

   1  $('a').each(function() {
   2  	var $a = $(this);
   3  	var href = $a.attr('href');
   4  	
   5  	if(typeof pageTracker == 'undefined') { return; }
   6  
   7  	// Link is external
   8  	if (href.match(/^http/) && !href.match(document.domain)) {
   9  		$a.click(function() {
  10  			pageTracker._trackPageview('/external/' + href);
  11  		});
  12  	} else {
  13  		$a.click(function() {
  14  			pageTracker._trackPageview('/internal' + href);
  15  		});
  16  	}
  17  });

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.

Tagged jquery, google, analytics, track, click

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

Ruby posted about 1 year ago by christian

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:

   1  Firefox/3.0 disable-tracking

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

   1  <% if !request.user_agent.include?('disable-tracking') %>
   2  TRACKING CODE GOES HERE
   3  <% 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.

Tagged google, analytics, tracking, exclude, traffic