@alexsab-ru/scripts 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +1 -6
- package/lib/analytics.js +22 -41
- package/package.json +1 -1
package/index.js
CHANGED
package/lib/analytics.js
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
|
-
|
|
1
|
+
window.dataLayer = window.dataLayer || [];
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
function dataLayer(event, t = {}) {
|
|
6
|
-
// console.log(event);
|
|
3
|
+
export function dataLayer(event, t = {}) {
|
|
4
|
+
console.log(event);
|
|
7
5
|
void 0 !== window.dataLayer && window.dataLayer.push({
|
|
8
6
|
event: event,
|
|
9
7
|
...t
|
|
10
8
|
})
|
|
11
9
|
}
|
|
12
10
|
|
|
13
|
-
|
|
11
|
+
export function getFormDataObject(formData, form_id) {
|
|
14
12
|
let obj = {"EventProperties":{}};
|
|
15
13
|
formData.forEach((value, key) => (obj["EventProperties"][key] = value));
|
|
16
14
|
obj['EventCategory'] = 'Lead';
|
|
@@ -19,7 +17,7 @@ export function WebsiteAnalytics() {
|
|
|
19
17
|
return obj;
|
|
20
18
|
}
|
|
21
19
|
|
|
22
|
-
|
|
20
|
+
export function ymGoal(goalName,goalParams) {
|
|
23
21
|
try {
|
|
24
22
|
Ya._metrika.getCounters().forEach((me)=>{
|
|
25
23
|
ym(me.id, "reachGoal", goalName, goalParams||{})
|
|
@@ -29,7 +27,7 @@ export function WebsiteAnalytics() {
|
|
|
29
27
|
}
|
|
30
28
|
}
|
|
31
29
|
|
|
32
|
-
|
|
30
|
+
export function ymPage(pageName,goalParams) {
|
|
33
31
|
try {
|
|
34
32
|
Ya._metrika.getCounters().forEach((me)=>{
|
|
35
33
|
ym(me.id, "hit", pageName, goalParams||{})
|
|
@@ -39,7 +37,7 @@ export function WebsiteAnalytics() {
|
|
|
39
37
|
}
|
|
40
38
|
}
|
|
41
39
|
|
|
42
|
-
|
|
40
|
+
export function addPhoneGoals(item) {
|
|
43
41
|
item.addEventListener('click', function(evt) {
|
|
44
42
|
dataLayer('phone-click');
|
|
45
43
|
});
|
|
@@ -51,7 +49,7 @@ export function WebsiteAnalytics() {
|
|
|
51
49
|
});
|
|
52
50
|
}
|
|
53
51
|
|
|
54
|
-
|
|
52
|
+
export function addEmailGoals(item) {
|
|
55
53
|
item.addEventListener('click', function(evt) {
|
|
56
54
|
dataLayer('email-click');
|
|
57
55
|
});
|
|
@@ -63,13 +61,14 @@ export function WebsiteAnalytics() {
|
|
|
63
61
|
});
|
|
64
62
|
}
|
|
65
63
|
|
|
64
|
+
document.querySelectorAll('a[href^\="tel:"]').forEach((tel)=>{
|
|
65
|
+
addPhoneGoals(tel);
|
|
66
|
+
});
|
|
67
|
+
document.querySelectorAll('a[href^\="mailto:"]').forEach((tel)=>{
|
|
68
|
+
addEmailGoals(tel);
|
|
69
|
+
});
|
|
70
|
+
|
|
66
71
|
var goals = [
|
|
67
|
-
{
|
|
68
|
-
selector: 'a[href^\="#common-modal"]',
|
|
69
|
-
action: 'click',
|
|
70
|
-
goal: 'form-open',
|
|
71
|
-
title: 'Открыли любую форму',
|
|
72
|
-
},
|
|
73
72
|
{
|
|
74
73
|
selector: 'form input',
|
|
75
74
|
action: 'click',
|
|
@@ -88,18 +87,19 @@ export function WebsiteAnalytics() {
|
|
|
88
87
|
goals.forEach(function(value, index, array){
|
|
89
88
|
if(value.goal != null) {
|
|
90
89
|
document.querySelectorAll(value.selector).forEach(function(element) {
|
|
91
|
-
|
|
90
|
+
console.log(["Set \"" + value.goal + "\" goal", element]);
|
|
92
91
|
element.addEventListener(value.action, function(){
|
|
93
|
-
dataLayer(
|
|
92
|
+
dataLayer("reachGoal", {
|
|
93
|
+
eventAction: value.goal,
|
|
94
|
+
title: value.title,
|
|
95
|
+
});
|
|
94
96
|
});
|
|
95
97
|
});
|
|
96
98
|
} else if(value.hit != null) {
|
|
97
99
|
document.querySelectorAll(value.selector).forEach(function(element) {
|
|
98
|
-
|
|
100
|
+
console.log(["Set \"" + value.goal + "\" hit", element]);
|
|
99
101
|
element.addEventListener(value.action, function(){
|
|
100
|
-
|
|
101
|
-
dataLayer.push({
|
|
102
|
-
event:"pageView",
|
|
102
|
+
dataLayer("pageView", {
|
|
103
103
|
eventAction: value.hit,
|
|
104
104
|
title: value.title,
|
|
105
105
|
});
|
|
@@ -109,22 +109,3 @@ export function WebsiteAnalytics() {
|
|
|
109
109
|
console.warn(["Ошибка в списке целей", value]);
|
|
110
110
|
}
|
|
111
111
|
});
|
|
112
|
-
|
|
113
|
-
document.querySelectorAll('a[href^\="tel:"]').forEach((tel)=>{
|
|
114
|
-
addPhoneGoals(tel);
|
|
115
|
-
});
|
|
116
|
-
document.querySelectorAll('a[href^\="mailto:"]').forEach((tel)=>{
|
|
117
|
-
addEmailGoals(tel);
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
// возвращаем объект с публичной функцией
|
|
121
|
-
return {
|
|
122
|
-
ymGoal: ymGoal,
|
|
123
|
-
ymPage: ymPage,
|
|
124
|
-
getFormDataObject: getFormDataObject,
|
|
125
|
-
dataLayer: dataLayer,
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
};
|
|
129
|
-
|
|
130
|
-
export default { WebsiteAnalytics }
|