DEV Community

Discussion on: Tracking Events with Google Analytics

Collapse
 
niharmore33 profile image
nihar • Edited

Overview

Events are user interactions with content that can be tracked independently from a web page or a screen load. Downloads, mobile ad clicks, gadgets, Flash elements, AJAX embedded elements, and video plays are all examples of actions you might want to track as Events.
You can also learn Google Analytics here: hackr.io/tutorials/learn-google-an...

If you're unfamiliar with events in Google Analytics you should first read the article About Events in the Analytics Help Center.

Implementation

Event hits can be sent using the send command and specifying a hitType of event. The send command has the following signature for the event hit type:

ga('send', 'event', [eventCategory], [eventAction], [eventLabel], [eventValue], [fieldsObject]);
Event fields

The following table summarizes the event fields:

Field Name Value Type Required Description
eventCategory text yes Typically the object that was interacted with (e.g. 'Video')
eventAction text yes The type of interaction (e.g. 'play')
eventLabel text no Useful for categorizing events (e.g. 'Fall Campaign')
eventValue integer no A numeric value associated with the event (e.g. 42)
For a more in-depth description of each of these fields, see Anatomy of an Event in the Analytics Help Center.

Examples:

The following command sends an event to Google Analytics indicating that the fall campaign promotional video was played:

ga('send', 'event', 'Videos', 'play', 'Fall Campaign');
Note that as with all send commands, the fields passed in the convenience parameters may also be specified in the fieldsObject. The above command could be rewritten as:

ga('send', {
hitType: 'event',
eventCategory: 'Videos',
eventAction: 'play',
eventLabel: 'Fall Campaign'
});
For more details, examples, and best practices for sending hits, see the Sending Data to Google Analytics guide. For more details on the calling signature of the send command, see the command queue reference.
Outbound link and form tracking

When a user clicks a link that points to another page on your site, that page typically sends a pageview hit as the user arrives. Because there's a series of pageviews, Google Analytics can figure out on the back end where the user navigated to (and from). But if a user clicks a link or submits a form to an external domain, that action is not captured unless you specifically tell Google Analytics what happened.

Outbound link and form tracking can be accomplished by sending events and specifying the destination URL in one of the event fields. The following event handler function can be used to send outbound link click events to Google Analytics:

function handleOutboundLinkClicks(event) {
ga('send', 'event', {
eventCategory: 'Outbound Link',
eventAction: 'click',
eventLabel: event.target.href
});
}
Tracking outbound links and forms can be tricky because most browsers will stop executing JavaScript on the current page once a new page starts to load. One solution to this problem is to set the transport field to beacon:

function handleOutboundLinkClicks(event) {
ga('send', 'event', {
eventCategory: 'Outbound Link',
eventAction: 'click',
eventLabel: event.target.href,
transport: 'beacon'
});
}

Collapse
 
traaidmark profile image
Adrian Kirsten

Hey bud,

Not sure I understand your comment? Is something wrong with my post?