Created
December 3, 2014 11:55
-
-
Save naoyeye/7e3d90a2bb4926ba7f81 to your computer and use it in GitHub Desktop.
GA 事件跟踪代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// GA 统计 | |
var trackEvent = function (opt) { | |
if (typeof ga !== 'undefined' && ga) { | |
var _act, _cat, _lbl, _val; | |
if (opt) { | |
_cat = opt.category || ''; | |
_act = opt.action || ''; | |
_lbl = opt.label || ''; | |
_val = opt.value || 0; | |
return ga('send', 'event', _cat, _act, _lbl, _val); | |
} | |
} | |
}; | |
if (typeof ga !== 'undefined' && ga) { | |
$('body').on('click', 'a[data-track]', function (e) { | |
var trackCode = $(this).data('track'); | |
var codes = trackCode.split('/'); | |
if (codes.length > 2) { | |
trackEvent({ | |
category: codes[0], | |
action: codes[1], | |
label: codes[2] | |
}); | |
} | |
}); | |
} | |
// ================== | |
// js 中调用 | |
trackEvent({category: 'campaign名称', action: '动作名称', label: '参数'}); | |
// 或者 | |
// html 中 | |
a(href='https://itunes.apple.com/cn/app/gei-hai-zi-shi-gei-hai-zi/id886752849?mt=8', target='_blank', data-track='campaign名称/动作名称/参数') | |
// ================== | |
// demo | |
// js: | |
// 打开某个应用 | |
$('body').on('click', 'a.open', function () { | |
// 获取包名 | |
var packageName = $(this).parents('.app-card').eq(0).attr('data-packageName'); | |
if (packageName) { | |
trackEvent({category: 'buzz', action: 'openAPP', label: | |
packageName}); | |
// P4 中打开对应 APP | |
window.campaignPlugin.openApp(packageName); | |
} | |
}); | |
// html | |
//- 下载按钮 | |
a(href='https://itunes.apple.com/cn/app/gei-hai-zi-shi-gei-hai-zi/id886752849?mt=8', data-track='tochild/download/iOS') | |
i.device | |
span 下载「给孩子的诗」 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment