@blotoutio/providers-google-analytics-4-sdk 0.6.5 → 0.6.7
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 +26 -4
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,13 +1,35 @@
|
|
|
1
1
|
var ProvidersGoogleAnalytics4Sdk = (function () {
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
|
+
const initGA4 = (ID) => {
|
|
5
|
+
window.dataLayer = window.dataLayer || [];
|
|
6
|
+
window.gtag = function gtag() {
|
|
7
|
+
// eslint-disable-next-line prefer-rest-params
|
|
8
|
+
window.dataLayer.push(arguments);
|
|
9
|
+
};
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
11
|
+
// @ts-ignore
|
|
12
|
+
window.gtag('js', new Date());
|
|
13
|
+
const element = document.createElement('script');
|
|
14
|
+
element.async = !0;
|
|
15
|
+
element.src = `https://www.googletagmanager.com/gtag/js?id=${ID}`;
|
|
16
|
+
const script = document.getElementsByTagName('script')[0];
|
|
17
|
+
if (script && script.parentNode) {
|
|
18
|
+
script.parentNode.insertBefore(element, script);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
4
21
|
const init = ({ manifest, userId }) => {
|
|
5
|
-
if (!manifest.variables || !window
|
|
22
|
+
if (!manifest.variables || !window) {
|
|
6
23
|
return;
|
|
7
24
|
}
|
|
8
|
-
window.gtag
|
|
9
|
-
|
|
10
|
-
}
|
|
25
|
+
if (!window.gtag && manifest.variables['enableBrowser'] === '1') {
|
|
26
|
+
initGA4(manifest.variables['measurementId']);
|
|
27
|
+
}
|
|
28
|
+
if (window.gtag) {
|
|
29
|
+
window.gtag('config', manifest.variables['measurementId'], {
|
|
30
|
+
user_id: userId,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
11
33
|
};
|
|
12
34
|
|
|
13
35
|
const data = {
|