[!NOTE]
analytics.js(Universal Analytics) andga.jsare legacy Google Analytics libraries superseded by Google Analytics 4 (GA4 viagtag.js). This guide serves as an archival reference.
Universal Analytics (analytics.js)
Pageviews
ga('create', 'UA-XXXX-Y', 'auto')
ga('create', 'UA-XXXX-Y', { userId: 'USER_ID' })
ga('send', 'pageview')
ga('send', 'pageview', { 'dimension15': 'My custom dimension' })
Event Tracking
ga('send', 'event', 'button', 'click', { color: 'red' });
ga('send', 'event', 'button', 'click', 'nav buttons', 4);
/* ^category ^action ^label ^value (int) */
Exception Tracking
ga('send', 'exception', {
exDescription: 'DatabaseError',
exFatal: false,
appName: 'myapp',
appVersion: '0.1.2'
})
Classic Google Analytics (ga.js)
Archived syntax for legacy synchronous/asynchronous ga.js tracking snippet implementations.
Event Tracking
// [..., category, action, label, value (int), noninteraction (bool)]
_gaq.push(['_trackEvent', 'Videos', 'Play', 'Birthday video', true]);
_gaq.push(['_trackEvent', 'Projects', 'Donate', 'Project name']);
_gaq.push(['_trackEvent', 'Accounts', 'Login']);
Custom Variables
// [..., index (1-5), name, value, scope (optional)]
_gaq.push(['_setCustomVar', 1, 'Logged in', 'Yes', 2]);
// Scope: 1 (visitor), 2 (session), 3 (page, default)