Last active
December 15, 2015 11:49
-
-
Save bobchao/5255557 to your computer and use it in GitHub Desktop.
測試 Universala Analytics
This file contains hidden or 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
var https = require('https'); | |
var querystring = require('querystring'); | |
function ga() { | |
var _useragent = 'GUATester/1.0 GUA/1.0'; | |
var _param = { | |
v: '1', | |
tid: '%replace_with_your_UA_id%', | |
cid: '' | |
}; | |
var _call = function (param){ | |
var post_data = querystring.stringify(_param)+'&'+querystring.stringify(param); | |
var post_options = { | |
host : 'www.google-analytics.com', | |
path : '/collect', | |
method : 'POST', | |
headers: { | |
'User-Agent': _useragent, | |
'Content-Length': post_data.length | |
} | |
} | |
var reqPost = https.request(post_options, function(res) { | |
console.log("statusCode: ", res.statusCode); | |
}); | |
reqPost.write(post_data); | |
reqPost.end(); | |
console.log("\nSend: " + post_data); | |
reqPost.on('error', function(e) { | |
console.error(e); | |
}); | |
} | |
this.sendEvent = function (eventCategory, eventAction, eventLabel){ | |
return _call({ | |
t: 'event', | |
ec: eventCategory, | |
ea: eventAction, | |
el: eventLabel | |
}); | |
} | |
this.sendOfflineEvent = function (eventCategory, eventAction, eventLabel){ | |
//send event which happended 3 hours 50 minutes ago. | |
return _call({ | |
t: 'event', | |
ec: eventCategory, | |
ea: eventAction, | |
el: eventLabel, | |
qt: (3*60+50)*60*1000 | |
}); //back to the future | |
} | |
this.setClientId = function (cid){ | |
_param.cid = cid; | |
console.log('\n\nSet client ID = ' + this.getClientId() + '\n\n'); | |
} | |
this.getClientId = function (){ | |
return _param.cid; | |
} | |
} | |
var ga_c = new ga(); | |
// 基本測試 | |
ga_c.setClientId('%replace_with_your_client_id%'); | |
// 跨設備、網站追蹤是可行的嗎? | |
ga_c.sendEvent('Test', 'Send', ga_c.getClientId()); | |
ga_c.sendEvent('Test', 'Send-2', ga_c.getClientId()); | |
ga_c.sendEvent('Test', 'Send-3', ga_c.getClientId()); | |
// 離線送資料的確實方式 | |
ga_c.sendOfflineEvent('Test', 'Offline-Send', ga_c.getClientId()); | |
// 更換 UUID 時,會怎麼計算? | |
ga_c.setClientId('%replace_with_your_2nd_client_id%'); | |
ga_c.sendEvent('Test', 'Send-4', ga_c.getClientId()); | |
ga_c.sendEvent('Test', 'Send-5', ga_c.getClientId()); | |
ga_c.sendEvent('Test', 'Send-6', ga_c.getClientId()); | |
//*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment