@alexsab-ru/scripts 0.12.0 → 0.13.0

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.
Files changed (2) hide show
  1. package/lib/analytics.js +95 -0
  2. package/package.json +1 -1
package/lib/analytics.js CHANGED
@@ -2,17 +2,39 @@ window.dataLayer = window.dataLayer || [];
2
2
 
3
3
  export function reachGoal(eventAction, t = {}) {
4
4
  t.eventAction = eventAction;
5
+ if(isGTMInstalled()) {
5
6
  dl("reachGoal-"+eventAction, {
6
7
  ...t
7
8
  })
9
+ } else {
10
+ if(checkGA4InDataLayer()) {
11
+ gGoal(eventAction, {
12
+ ...t
13
+ });
14
+ }
15
+ ymGoal(eventAction, {
16
+ ...t
17
+ });
8
18
  }
19
+ }
9
20
 
10
21
  export function pageView(eventAction, t = {}) {
11
22
  t.eventAction = eventAction;
23
+ if(isGTMInstalled()) {
12
24
  dl("pageView-"+eventAction, {
13
25
  ...t
14
26
  })
27
+ } else {
28
+ if(checkGA4InDataLayer()) {
29
+ gGoal(eventAction, {
30
+ ...t
31
+ });
32
+ }
33
+ ymPage(eventAction, {
34
+ ...t
35
+ });
15
36
  }
37
+ }
16
38
 
17
39
  export function dl(event, t = {}) {
18
40
  // console.log(event, t);
@@ -33,7 +55,17 @@ export function getFormDataObject(formData, form_id) {
33
55
  };
34
56
  }
35
57
 
58
+ export function gGoal(goalName,goalParams){
59
+ try {
60
+ dl(goalName, goalParams)
61
+ } catch (err) {
62
+ console.error(goalName + ' - error send goal to Google Analytics');
63
+ }
64
+ }
65
+
66
+
36
67
  export function ymGoal(goalName,goalParams) {
68
+ // console.log("ymGoal:", goalName,goalParams);
37
69
  try {
38
70
  Ya._metrika.getCounters().forEach((me)=>{
39
71
  ym(me.id, "reachGoal", goalName, goalParams||{})
@@ -111,3 +143,66 @@ export function addClickCopyContextmenuGoals(item, prefix) {
111
143
  console.warn("Ошибка в списке целей", value);
112
144
  }
113
145
  });
146
+
147
+ function isGTMInstalled() {
148
+ // console.log("gtm.js:", window.dataLayer.length > 0 && dataLayer[0].event == "gtm.js");
149
+ // console.log("window.isGTMInstalled:", window.isGTMInstalled);
150
+ if (window.dataLayer.length > 0 && dataLayer[0].event == "gtm.js") return true;
151
+ switch(window.isGTMInstalled) {
152
+ case true:
153
+ return true;
154
+ break;
155
+ case false:
156
+ return false;
157
+ break;
158
+ default:
159
+ }
160
+
161
+ let scripts = document.querySelectorAll('script');
162
+ for (let script of scripts) {
163
+ if (script.src && script.src.includes('gtm.js')) {
164
+ window.isGTMInstalled = true;
165
+ return true;
166
+ }
167
+ }
168
+ window.isGTMInstalled = false;
169
+ return false;
170
+ }
171
+
172
+ function checkGA4InDataLayer() {
173
+ // console.log("window.ga4Installed:", window.ga4Installed);
174
+ switch(window.ga4Installed) {
175
+ case true:
176
+ return true;
177
+ break;
178
+ case false:
179
+ return false;
180
+ break;
181
+ default:
182
+ }
183
+
184
+ if (typeof window.dataLayer !== 'undefined') {
185
+ // Просмотр событий в dataLayer для поиска конфигурации GA4
186
+ const ga4Config = window.dataLayer.find(item =>
187
+ item && item[0] === 'config' && item[1] && item[1].startsWith('G-')
188
+ );
189
+
190
+ if (ga4Config) {
191
+ // console.log('GA4 настройка найдена в dataLayer:', ga4Config[1]);
192
+ window.ga4Installed = true;
193
+ return true;
194
+ }
195
+ }
196
+
197
+ const scripts = document.querySelectorAll('script');
198
+ scripts.forEach(script => {
199
+ if (script.src && script.src.includes('gtag.js')) {
200
+ window.ga4Installed = true;
201
+ return true;
202
+ }
203
+ });
204
+
205
+ // console.log('GA4 конфигурация не найдена в dataLayer');
206
+ window.ga4Installed = false;
207
+ return false;
208
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alexsab-ru/scripts",
3
- "version": "0.12.0",
3
+ "version": "0.13.0",
4
4
  "description": "common libs for websites",
5
5
  "main": "index.js",
6
6
  "scripts": {