Cheat Sheet速查表
跳转到主要内容

Google Analytics 历史接口(analytics.js / ga.js) 速查表

本文根据 Devhints / Google Analytics’s analytics.js 翻译。代码示例及版本范围沿用原文。原文标注更新日期:2017-10-29。

Google Analytics 中的 analytics.js 已弃用。

页面浏览量

ga('create', 'UA-XXXX-Y', 'auto')
ga('create', 'UA-XXXX-Y', { userId: 'USER_ID' })
ga('send', 'pageview')
ga('send', 'pageview', { 'dimension15': 'My custom dimension' })

事件

ga('send', 'event', 'button',  'click', {color: 'red'});
ga('send', 'event', 'button',  'click', 'nav buttons',  4);
/*                  ^category  ^action  ^label          ^value */

异常

ga('send', 'exception', {
  exDescription: 'DatabaseError',
  exFatal: false,
  appName: 'myapp',
  appVersion: '0.1.2'
})

旧版 ga.js 接口

本节补充自 Devhints / Google Analytics,重复的 analytics.js 示例已省略。

页面浏览量

事件追踪

// ga.js
// [..., 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'])

变量

// [..., index, name, value, scope (optional)]
_gaq.push(['_setCustomVar', 1, 'Logged in', 'Yes', 2]);

// Scope = 1 (visitor), 2 (session), 3 (page, default)

参考资料