Hands up if you use Google Analytics. Actually, it’s probably easier to ask who doesn’t use Analytics. Whatever your opinion of Google and its plans for world domination, Analytics is an amazing tool that has revolutionized web statistics.
For most tasks, the standard page-tracking code is adequate:
Identify Your Requirements
Google Analytics also provides comprehensive ecommerce tracking systems. This is more appropriate for when you’re selling items, so keep reading sitepoint.com for a tutorial coming soon.
The first data item to consider is the category—a single name for all events of a particular type. In this case, we could use “mp3system.” You could use the same category name for every event on your web page, or different categories, depending on the widget being used.
- search (searching for a song using a key phrase)
- view (viewing information about an MP3)
- download (starting an MP3 download)
A label is an optional description of the action that occurred. In the case of searching, we could use the search phrase as our label. For viewing or downloading, we could apply the song ID and/or title for our label.
The optional value provides a numeric quantity for the event. For MP3s, we might provide the file size or track length, so that we can report total and average download statistics. In other situations, you could record a monetary amount, or the time taken to complete an action.
if (_gaq) _gaq.push(['_trackEvent', category, action, label, value]);
_gaq.push(['_trackEvent', 'mp3system', 'search', 'good songs']); _gaq.push(['_trackEvent', 'mp3system', 'view', 'song 1', 3456]); _gaq.push(['_trackEvent', 'mp3system', 'download', 'song 1', 3456]);
For a file download, the simplest HTML would be:
<a href="file.mp3" onclick="_gaq.push(['_trackEvent', 'mp3system', 'download', 'file.mp3']);return true;">download</a>
Your custom event statistics should be available on Google Analytics within 24 hours. From your profile, select Content followed by Event Tracking.
Google Analytics event tracking can help you monitor user activity at a deeper level than standard page views. Are you using it for any interesting applications? Will you implement the code in your next project?
If you want to read more from Craig, subscribe to our weekly tech geek newsletter, Tech Times.
- 1 Fetching Data from a Third-Party API with Vue.js and Axios
- 2 How to Improve Site Performance (and Conversions) with Dareboost
- 3 An Alternative Laravel Package Development Workflow
- 4 Create Your Own Yeoman-Style Scaffolding Tool with Caporal.js
- 5 Make Your Own Social Network, Game Server, or Knowledgebase! - Sourcehunt