@alexsab-ru/scripts 0.0.2
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/AUTHORS +1 -0
- package/LICENSE +21 -0
- package/README.md +36 -0
- package/index.js +6 -0
- package/lib/analytics.js +130 -0
- package/package.json +29 -0
package/AUTHORS
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Alexander Sabanov <info@alexsab.ru>
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Alexander Sabanov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# scripts
|
|
2
|
+
common libs for our websites
|
|
3
|
+
|
|
4
|
+
## Install and update
|
|
5
|
+
```bash
|
|
6
|
+
pnpm i https://github.com/alexsab-ru/scripts
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
### Send goals Analytics.js
|
|
10
|
+
|
|
11
|
+
```js
|
|
12
|
+
import '/node_modules/scripts/js/analytics.js';
|
|
13
|
+
|
|
14
|
+
window.WebsiteAnalytics.dataLayer("phone-click");
|
|
15
|
+
window.WebsiteAnalytics.dataLayer("phone-copy");
|
|
16
|
+
window.WebsiteAnalytics.dataLayer("phone-contextmenu");
|
|
17
|
+
window.WebsiteAnalytics.dataLayer("email-click");
|
|
18
|
+
window.WebsiteAnalytics.dataLayer("email-copy");
|
|
19
|
+
window.WebsiteAnalytics.dataLayer("email-contextmenu");
|
|
20
|
+
window.WebsiteAnalytics.dataLayer("video-click");
|
|
21
|
+
window.WebsiteAnalytics.dataLayer("form-open");
|
|
22
|
+
window.WebsiteAnalytics.dataLayer("form-submit");
|
|
23
|
+
window.WebsiteAnalytics.dataLayer("form-required");
|
|
24
|
+
window.WebsiteAnalytics.dataLayer("form-submit");
|
|
25
|
+
window.WebsiteAnalytics.dataLayer("form-error");
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Insert before send
|
|
29
|
+
```js
|
|
30
|
+
var formDataObj = window.WebsiteAnalytics.getFormDataObject(formData, form.id);
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Insert when Success callback
|
|
34
|
+
```js
|
|
35
|
+
window.WebsiteAnalytics.dataLayer("form-success", formDataObj);
|
|
36
|
+
```
|
package/index.js
ADDED
package/lib/analytics.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
export function WebsiteAnalytics() {
|
|
2
|
+
|
|
3
|
+
window.dataLayer = window.dataLayer || [];
|
|
4
|
+
|
|
5
|
+
function dataLayer(event, t = {}) {
|
|
6
|
+
// console.log(event);
|
|
7
|
+
void 0 !== window.dataLayer && window.dataLayer.push({
|
|
8
|
+
event: event,
|
|
9
|
+
...t
|
|
10
|
+
})
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function getFormDataObject(formData, form_id) {
|
|
14
|
+
let obj = {"EventProperties":{}};
|
|
15
|
+
formData.forEach((value, key) => (obj["EventProperties"][key] = value));
|
|
16
|
+
obj['EventCategory'] = 'Lead';
|
|
17
|
+
obj["EventProperties"]['formID'] = form_id;
|
|
18
|
+
obj['sourceName'] = 'page';
|
|
19
|
+
return obj;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function ymGoal(goalName,goalParams) {
|
|
23
|
+
try {
|
|
24
|
+
Ya._metrika.getCounters().forEach((me)=>{
|
|
25
|
+
ym(me.id, "reachGoal", goalName, goalParams||{})
|
|
26
|
+
})
|
|
27
|
+
} catch (err) {
|
|
28
|
+
console.error(goalName + ' - error send goal to Metrika');
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function ymPage(pageName,goalParams) {
|
|
33
|
+
try {
|
|
34
|
+
Ya._metrika.getCounters().forEach((me)=>{
|
|
35
|
+
ym(me.id, "hit", pageName, goalParams||{})
|
|
36
|
+
})
|
|
37
|
+
} catch (err) {
|
|
38
|
+
console.error(goalName + ' - error send page to Metrika');
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function addPhoneGoals(item) {
|
|
43
|
+
item.addEventListener('click', function(evt) {
|
|
44
|
+
dataLayer('phone-click');
|
|
45
|
+
});
|
|
46
|
+
item.addEventListener('copy', function(evt) {
|
|
47
|
+
dataLayer('phone-copy');
|
|
48
|
+
});
|
|
49
|
+
item.addEventListener('contextmenu', function(evt) {
|
|
50
|
+
dataLayer('phone-contextmenu');
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function addEmailGoals(item) {
|
|
55
|
+
item.addEventListener('click', function(evt) {
|
|
56
|
+
dataLayer('email-click');
|
|
57
|
+
});
|
|
58
|
+
item.addEventListener('copy', function(evt) {
|
|
59
|
+
dataLayer('email-copy');
|
|
60
|
+
});
|
|
61
|
+
item.addEventListener('contextmenu', function(evt) {
|
|
62
|
+
dataLayer('email-contextmenu');
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
var goals = [
|
|
67
|
+
{
|
|
68
|
+
selector: 'a[href^\="#common-modal"]',
|
|
69
|
+
action: 'click',
|
|
70
|
+
goal: 'form-open',
|
|
71
|
+
title: 'Открыли любую форму',
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
selector: 'form input',
|
|
75
|
+
action: 'click',
|
|
76
|
+
goal: 'form-click',
|
|
77
|
+
title: 'Клик в поле любой формы',
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
selector: 'form input',
|
|
81
|
+
action: 'change',
|
|
82
|
+
goal: 'form-change',
|
|
83
|
+
title: 'Изменения полей любой формы',
|
|
84
|
+
},
|
|
85
|
+
|
|
86
|
+
];
|
|
87
|
+
|
|
88
|
+
goals.forEach(function(value, index, array){
|
|
89
|
+
if(value.goal != null) {
|
|
90
|
+
document.querySelectorAll(value.selector).forEach(function(element) {
|
|
91
|
+
// console.log("Set \"" + value.goal + "\" goal");
|
|
92
|
+
element.addEventListener(value.action, function(){
|
|
93
|
+
dataLayer(value.goal);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
} else if(value.hit != null) {
|
|
97
|
+
document.querySelectorAll(value.selector).forEach(function(element) {
|
|
98
|
+
// console.log("Set \"" + value.goal + "\" hit");
|
|
99
|
+
element.addEventListener(value.action, function(){
|
|
100
|
+
// ymPage(value.goal);
|
|
101
|
+
dataLayer.push({
|
|
102
|
+
event:"pageView",
|
|
103
|
+
eventAction: value.hit,
|
|
104
|
+
title: value.title,
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
} else {
|
|
109
|
+
console.warn(["Ошибка в списке целей", value]);
|
|
110
|
+
}
|
|
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 }
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@alexsab-ru/scripts",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "common libs for websites",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/alexsab-ru/scripts.git"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"analytics"
|
|
15
|
+
],
|
|
16
|
+
"author": {
|
|
17
|
+
"name": "Alexander Sabanov",
|
|
18
|
+
"email": "info@alexsab.ru",
|
|
19
|
+
"url": "https://alexsab.ru"
|
|
20
|
+
},
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/alexsab-ru/scripts/issues"
|
|
24
|
+
},
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"homepage": "https://github.com/alexsab-ru/scripts#readme"
|
|
29
|
+
}
|