@alexsab-ru/scripts 0.0.2 → 0.0.4

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 CHANGED
@@ -1,6 +1 @@
1
- // index.js
2
- const analytics = require('./lib/analytics');
3
-
4
- module.exports = {
5
- analytics
6
- };
1
+ export * from './lib/analytics';
package/lib/analytics.js CHANGED
@@ -1,25 +1,37 @@
1
- export function WebsiteAnalytics() {
1
+ window.dataLayer = window.dataLayer || [];
2
2
 
3
- window.dataLayer = window.dataLayer || [];
3
+ export function reachGoal(eventAction, t = {}) {
4
+ t.eventAction = eventAction;
5
+ dl("reachGoal-"+eventAction, {
6
+ ...t
7
+ })
8
+ }
9
+
10
+ export function pageView(eventAction, t = {}) {
11
+ t.eventAction = eventAction;
12
+ dl("pageView-"+eventAction, {
13
+ ...t
14
+ })
15
+ }
4
16
 
5
- function dataLayer(event, t = {}) {
6
- // console.log(event);
17
+ export function dl(event, t = {}) {
18
+ console.log(event, t);
7
19
  void 0 !== window.dataLayer && window.dataLayer.push({
8
20
  event: event,
9
21
  ...t
10
22
  })
11
23
  }
12
24
 
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;
25
+ export function getFormDataObject(formData, form_id) {
26
+ let obj = {"eventProperties":{}};
27
+ formData.forEach((value, key) => (obj["eventProperties"][key] = value));
28
+ obj['eventCategory'] = 'Lead';
29
+ obj["eventProperties"]['formID'] = form_id;
18
30
  obj['sourceName'] = 'page';
19
31
  return obj;
20
32
  }
21
33
 
22
- function ymGoal(goalName,goalParams) {
34
+ export function ymGoal(goalName,goalParams) {
23
35
  try {
24
36
  Ya._metrika.getCounters().forEach((me)=>{
25
37
  ym(me.id, "reachGoal", goalName, goalParams||{})
@@ -29,7 +41,7 @@ export function WebsiteAnalytics() {
29
41
  }
30
42
  }
31
43
 
32
- function ymPage(pageName,goalParams) {
44
+ export function ymPage(pageName,goalParams) {
33
45
  try {
34
46
  Ya._metrika.getCounters().forEach((me)=>{
35
47
  ym(me.id, "hit", pageName, goalParams||{})
@@ -39,37 +51,38 @@ export function WebsiteAnalytics() {
39
51
  }
40
52
  }
41
53
 
42
- function addPhoneGoals(item) {
54
+ export function addPhoneGoals(item) {
43
55
  item.addEventListener('click', function(evt) {
44
- dataLayer('phone-click');
56
+ reachGoal('phone_click');
45
57
  });
46
58
  item.addEventListener('copy', function(evt) {
47
- dataLayer('phone-copy');
59
+ reachGoal('phone_copy');
48
60
  });
49
61
  item.addEventListener('contextmenu', function(evt) {
50
- dataLayer('phone-contextmenu');
62
+ reachGoal('phone_contextmenu');
51
63
  });
52
64
  }
53
65
 
54
- function addEmailGoals(item) {
66
+ export function addEmailGoals(item) {
55
67
  item.addEventListener('click', function(evt) {
56
- dataLayer('email-click');
68
+ reachGoal('email_click');
57
69
  });
58
70
  item.addEventListener('copy', function(evt) {
59
- dataLayer('email-copy');
71
+ reachGoal('email_copy');
60
72
  });
61
73
  item.addEventListener('contextmenu', function(evt) {
62
- dataLayer('email-contextmenu');
74
+ reachGoal('email_contextmenu');
63
75
  });
64
76
  }
65
77
 
78
+ document.querySelectorAll('a[href^\="tel:"]').forEach((tel)=>{
79
+ addPhoneGoals(tel);
80
+ });
81
+ document.querySelectorAll('a[href^\="mailto:"]').forEach((tel)=>{
82
+ addEmailGoals(tel);
83
+ });
84
+
66
85
  var goals = [
67
- {
68
- selector: 'a[href^\="#common-modal"]',
69
- action: 'click',
70
- goal: 'form-open',
71
- title: 'Открыли любую форму',
72
- },
73
86
  {
74
87
  selector: 'form input',
75
88
  action: 'click',
@@ -88,43 +101,23 @@ export function WebsiteAnalytics() {
88
101
  goals.forEach(function(value, index, array){
89
102
  if(value.goal != null) {
90
103
  document.querySelectorAll(value.selector).forEach(function(element) {
91
- // console.log("Set \"" + value.goal + "\" goal");
104
+ console.log("Set \"" + value.goal + "\" goal", element);
92
105
  element.addEventListener(value.action, function(){
93
- dataLayer(value.goal);
106
+ reachGoal(value.goal, {
107
+ title: value.title,
108
+ });
94
109
  });
95
110
  });
96
111
  } else if(value.hit != null) {
97
112
  document.querySelectorAll(value.selector).forEach(function(element) {
98
- // console.log("Set \"" + value.goal + "\" hit");
113
+ console.log("Set \"" + value.goal + "\" hit", element);
99
114
  element.addEventListener(value.action, function(){
100
- // ymPage(value.goal);
101
- dataLayer.push({
102
- event:"pageView",
103
- eventAction: value.hit,
115
+ pageView(value.hit, {
104
116
  title: value.title,
105
117
  });
106
118
  });
107
119
  });
108
120
  } else {
109
- console.warn(["Ошибка в списке целей", value]);
121
+ console.warn("Ошибка в списке целей", value);
110
122
  }
111
123
  });
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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alexsab-ru/scripts",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "common libs for websites",
5
5
  "main": "index.js",
6
6
  "scripts": {