Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
サーバーログでイベントトラッキングする用のJSライブラリ(JS版)
(function() {
var EvTrack, url;
EvTrack = (function() {
function EvTrack(url) {
this.url = url;
this.opt = {
category: "",
action: "",
label: "",
value: ""
};
this.optName = {
category: "cat",
action: "act",
label: "lab",
value: "val"
};
}
EvTrack.prototype.begin = function(param) {
var ele, objBody;
objBody = document.getElementsByTagName("body").item(0);
ele = document.createElement("img");
ele.src = this.url + "?" + param;
return objBody.appendChild(ele);
};
EvTrack.prototype.makeParam = function() {
var str;
str = "r=" + (Math.round(Math.random() * (new Date).getTime() / 1000));
str += "&" + this.optName['category'] + "=" + (encodeURIComponent(this.opt['category']));
str += "&" + this.optName['action'] + "=" + (encodeURIComponent(this.opt['action']));
str += "&" + this.optName['label'] + "=" + (encodeURIComponent(this.opt['label']));
if (this.opt['value'] !== "" && (this.opt["value"] != null)) {
str += "&" + this.optName['value'] + "=" + (encodeURIComponent(this.opt['value']));
}
return str;
};
EvTrack.prototype.parseArr = function(arr) {
if (arr.length === 3 || arr.length === 4) {
this.opt = {
category: arr[0],
action: arr[1],
label: arr[2]
};
if (arr[3] != null) this.opt.value = arr[3];
}
return this.opt;
};
EvTrack.prototype.track = function(arr) {
var param;
if (arr != null) this.parseArr(arr);
param = this.makeParam();
this.begin(param);
this.clear();
return false;
};
EvTrack.prototype.clear = function() {
return this.opt = {
category: "",
action: "",
label: "",
value: ""
};
};
return EvTrack;
})();
//サーバーに置いてあるビーコン画像URLを記載してください。
url = "b.gif";
window._evTrac = new EvTrack(url);
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment