Skip to content

Instantly share code, notes, and snippets.

@ronfe
Created December 1, 2015 03:03
Show Gist options
  • Save ronfe/b546f85b8d76d4d908d6 to your computer and use it in GitHub Desktop.
Save ronfe/b546f85b8d76d4d908d6 to your computer and use it in GitHub Desktop.
// GA closure
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
// ua-host mapping
// Use the very first letter in window.location.hostname as key
// TODO: replace with the right GA ua for m.yangcong and vs.yangcong
var mapping = {
'y': 'UA-66876862-1',
'w': 'UA-66876862-1',
'm': 'UA-66876862-1',
'v': 'UA-66876862-1'
};
// Configure GA ua
var hostCategory = window.location.hostname[0]
var ua = mapping[hostCategory]
ga('create', ua, 'auto');
var recordPoint = function(user, school, point){
ga('set', '&uid', user._id);
ga('set', 'dimension2', user.role);//设置用户角色
ga('set', 'dimension1', user.usefulData.q);//设置用户渠道
ga('set', 'dimension4', user.usefulData.from);//设置用户创建方式
ga('set', 'dimension3', user.profile.gender);//设置用户性别
// To be considered
//ga('set', 'dimension5', sc)
// TODO: change key name when backend make sure
ga('set', 'dimension6', user.activateDate);
// Send To GA
var eventType = '';
if (pointData.eventKey.indexOf('click') === 0){
// This is the click event.
eventType = 'click';
}
// else if (pointData.eventKey.indexOf('enter') === 0){
// // This is the enter event.
// eventType = ''
//
// }
else {
eventType = 'other'
}
var gaPointData = JSON.parse(JSON.stringify(pointData));
var gaEvent = gaPointData.eventKey;
var gaSuccess = false;
delete gaPointData.eventKey;
ga('send', {
'hitType': 'event',
'eventCategory': eventType,
'eventAction': gaEvent,
'eventValue': JSON.stringify(gaPointData),
'hitCallback': function(){
// Send the point
gaSuccess = true
}
});
setTimeOut
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment