forked from OpenShare/openshare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalytics.js
107 lines (93 loc) · 3 KB
/
analytics.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
export default function (type, cb) {// eslint-disable-line
const isGA = type === 'event' || type === 'social';
const isTagManager = type === 'tagManager';
if (isGA) checkIfAnalyticsLoaded(type, cb);
if (isTagManager) setTagManager(cb);
}
function checkIfAnalyticsLoaded(type, cb) {
if (window.ga) {
if (cb) cb();
// bind to shared event on each individual node
listen((e) => {
const platform = e.target.getAttribute('data-open-share');
const target = e.target.getAttribute('data-open-share-link') ||
e.target.getAttribute('data-open-share-url') ||
e.target.getAttribute('data-open-share-username') ||
e.target.getAttribute('data-open-share-center') ||
e.target.getAttribute('data-open-share-search') ||
e.target.getAttribute('data-open-share-body');
if (type === 'event') {
ga('send', 'event', { // eslint-disable-line no-undef
eventCategory: 'OpenShare Click',
eventAction: platform,
eventLabel: target,
transport: 'beacon',
});
}
if (type === 'social') {
ga('send', { // eslint-disable-line no-undef
hitType: 'social',
socialNetwork: platform,
socialAction: 'share',
socialTarget: target,
});
}
});
} else {
setTimeout(() => {
checkIfAnalyticsLoaded(type, cb);
}, 1000);
}
}
function setTagManager(cb) {
if (window.dataLayer && window.dataLayer[0]['gtm.start']) {
if (cb) cb();
listen(onShareTagManger);
getCounts((e) => {
const count = e.target ?
e.target.innerHTML :
e.innerHTML;
const platform = e.target ?
e.target.getAttribute('data-open-share-count-url') :
e.getAttribute('data-open-share-count-url');
window.dataLayer.push({
event: 'OpenShare Count',
platform,
resource: count,
activity: 'count',
});
});
} else {
setTimeout(() => {
setTagManager(cb);
}, 1000);
}
}
function listen(cb) {
// bind to shared event on each individual node
[].forEach.call(document.querySelectorAll('[data-open-share]'), (node) => {
node.addEventListener('OpenShare.shared', cb);
});
}
function getCounts(cb) {
const countNode = document.querySelectorAll('[data-open-share-count]');
[].forEach.call(countNode, (node) => {
if (node.textContent) cb(node);
else node.addEventListener(`OpenShare.counted-${node.getAttribute('data-open-share-count-url')}`, cb);
});
}
function onShareTagManger(e) {
const platform = e.target.getAttribute('data-open-share');
const target = e.target.getAttribute('data-open-share-link') ||
e.target.getAttribute('data-open-share-url') ||
e.target.getAttribute('data-open-share-username') ||
e.target.getAttribute('data-open-share-center') ||
e.target.getAttribute('data-open-share-search') ||
e.target.getAttribute('data-open-share-body');
window.dataLayer.push({
event: 'OpenShare Share',
platform,
resource: target,
activity: 'share',
});
}