Skip to content

Instantly share code, notes, and snippets.

@greypants
Created December 18, 2012 21:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greypants/4332412 to your computer and use it in GitHub Desktop.
Save greypants/4332412 to your computer and use it in GitHub Desktop.
GA: data-track attribute pattern

Basic Tracking

data-track

To add basic event tracking to an element on the page, add this attribute with the appropriate values you'd like to send, separated by pipes |.

<button data-track="category|action|opt_label|opt_value|opt_noninteraction">
	Basic Tracking
</button>

Optional attributes

data-track-delay

For every link or form with a data-track attribute that takes you to a new page (navigates somewhere else), add this attribute with a value of "true". This delays the loading of the new page for a 100ms to help ensure the _utm.gif request has adequate time to go through (for the event to be tracked).

<a href="link" data-track="category|action|opt_label" data-track-delay="true">
	Tracking with delayed navigation
</a>

data-track-method

To use alternate tracking for special cases like _trackSocial, add this attribute with the desired method name as the value. If this attribute is omitted, tracking defaults to _trackEvent.

<a href="facebook.com" data-track="category|action|opt_label" data-track-method="_trackSocial">
	Social Tracking
</a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment