-
Notifications
You must be signed in to change notification settings - Fork 0
/
tagmanager.js
155 lines (142 loc) · 4.69 KB
/
tagmanager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// module from: https://github.com/umdjs/umd/blob/master/templates/jqueryPlugin.js
(function(factory) {
if (typeof define === "function" && define.amd) {
define(["jquery"], factory);
} else if (typeof module === "object" && module.exports) {
module.exports = function(root, jQuery) {
if (jQuery === undefined) {
if (typeof window !== "undefined") {
jQuery = require("jquery");
} else {
jQuery = require("jquery")(root);
}
}
factory(jQuery);
return jQuery;
};
} else {
factory(jQuery);
}
}(function($) {
var id, debug = false;
var defaultSettings = {
where: "document",
class: "ga-tag",
event: "click",
debug: false
};
var _pushGA = function(obj) {
if (debug) {
console.log("[GTM] Push: ", obj);
} else {
return dataLayer.push(obj);
}
return false;
};
var _pushDataAttrGA = function(where, className, event) {
var eventSuffix = ".gaTagManager";
$(where)
.find(" ." + className)
.unbind(event + eventSuffix)
.bind(event + eventSuffix, function(e) {
var elem = $(e.currentTarget);
_pushGA(_createObjectFromElement(elem));
});
};
var _checkTagManagerIsPresent = function() {
var msg;
try {
if (window.dataLayer) {
return true;
}
} catch (err) {
msg = "[GTM] ERROR: Google Tag Manager is not present. Please check that the code is properly included.";
$.error(msg);
console.error(msg);
}
};
var _createGAEventObjectFromElement = function(elem) {
var obj = {
category: elem.data("gacategory"),
action: elem.data("gaaction"),
label: elem.data("galabel"),
event: elem.data("gaevent")
};
var custom = elem.data("gacustom");
if (custom !== undefined) {
$.each(custom, function(name, value) {
obj[name] = value;
});
}
return obj;
};
var _createVirtualUrlObjectFromElement = function(elem) {
var obj = {
virtualurl: elem.data("gaurl"),
event: elem.data("gaevent")
};
var custom = elem.data("gacustom");
if (custom !== undefined) {
$.each(custom, function(name, value) {
obj[name] = value;
});
}
return obj;
};
var _createObjectFromElement = function(elem) {
var event = elem.data("gaevent"),
actions = {
"virtualpage": _createVirtualUrlObjectFromElement,
"eventga": _createGAEventObjectFromElement
};
return actions[event].call(this, elem);
};
var _initCode = function(id) {
var f = document.getElementsByTagName("script")[0],
j = document.createElement("script");
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
"gtm.start": new Date().getTime(),
"event": "gtm.js"
});
j.async = true;
j.src = "//www.googletagmanager.com/gtm.js?id=" + id + "&l=" + "dataLayer";
f.parentNode.insertBefore(j, f);
};
var methods = {
init: function(options) {
var settings = $.extend(defaultSettings, options);
id = settings.id || id;
debug = settings.debug || debug;
if (!id) {
$.error("Expected a Google Tag Manager ID. Must be something like: GTM-ABC123");
return;
}
if (!window.dataLayer) {
if (debug) {
console.log("[GTM] Init: inserting Google Tag Manager code.");
}
_initCode(id);
}
_pushDataAttrGA(settings.where, settings.class, settings.event);
},
push: function(obj) {
if (_checkTagManagerIsPresent()) {
return _pushGA(obj);
}
return false;
}
};
$.fn.tagManager = function(method, data) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === "object" || !method) {
return methods.init.apply(this, arguments);
} else {
$.error("Method " + method + " does not exist on jQuery.tagManager");
}
};
$.tagManager = function(method, options) {
$.fn.tagManager(method, options);
};
}));