@uxf/analytics 10.1.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.
- package/README.md +64 -0
- package/consent/index.d.ts +1 -0
- package/consent/index.js +17 -0
- package/gtm/index.d.ts +3 -0
- package/gtm/index.js +18 -0
- package/package.json +23 -0
- package/src/consent/index.d.ts +7 -0
- package/src/consent/index.js +17 -0
- package/src/gtm/gmt-script.d.ts +6 -0
- package/src/gtm/gmt-script.js +29 -0
- package/src/gtm/types.d.ts +15 -0
- package/src/gtm/types.js +2 -0
- package/src/gtm/utils.d.ts +2 -0
- package/src/gtm/utils.js +20 -0
package/README.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# @uxf/analytics
|
|
2
|
+
[](https://www.npmjs.com/package/@uxf/analytics)
|
|
3
|
+
[](https://www.npmjs.com/package/@uxf/analytics)
|
|
4
|
+
[](https://www.npmjs.com/package/@uxf/analytics)
|
|
5
|
+
[](https://www.npmjs.com/package/@uxf/analytics)
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
yarn add @uxf/analytics
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @uxf/analytics
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Cookie consent
|
|
18
|
+
|
|
19
|
+
### Store consent to cookie
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { storeConsentToCookie } from "@uxf/analytics/consent";
|
|
23
|
+
|
|
24
|
+
storeConsentToCookie({
|
|
25
|
+
analytics_storage: true,
|
|
26
|
+
ad_storage: false,
|
|
27
|
+
personalization_storage: false,
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Read consent from cookie
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import { readConsentFromCookie } from "@uxf/analytics/consent";
|
|
35
|
+
|
|
36
|
+
const consent = readConsentFromCookie();
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## GTM
|
|
40
|
+
|
|
41
|
+
### Initialize GTM
|
|
42
|
+
|
|
43
|
+
This reads the consent from cookie and initializes the GTM dataLayer.
|
|
44
|
+
|
|
45
|
+
```tsx
|
|
46
|
+
import { GtmScript } from "@uxf/analytics/gtm";
|
|
47
|
+
|
|
48
|
+
// in your head component
|
|
49
|
+
<GtmScript gtmId="GTM-YOURID" />
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Update GTM consent
|
|
53
|
+
|
|
54
|
+
This stores the consent to cookie and updates the GTM dataLayer.
|
|
55
|
+
|
|
56
|
+
```tsx
|
|
57
|
+
import { updateGtmConsent } from "@uxf/analytics/gtm";
|
|
58
|
+
|
|
59
|
+
updateGtmConsent({
|
|
60
|
+
analytics_storage: true,
|
|
61
|
+
ad_storage: false,
|
|
62
|
+
personalization_storage: false,
|
|
63
|
+
});
|
|
64
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../src/consent";
|
package/consent/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("../src/consent"), exports);
|
package/gtm/index.d.ts
ADDED
package/gtm/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("../src/gtm/gmt-script"), exports);
|
|
18
|
+
__exportStar(require("../src/gtm/utils"), exports);
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@uxf/analytics",
|
|
3
|
+
"version": "10.1.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"build": "tsc -P tsconfig.json",
|
|
7
|
+
"typecheck": "tsc --noEmit --skipLibCheck"
|
|
8
|
+
},
|
|
9
|
+
"publishConfig": {
|
|
10
|
+
"access": "public"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git"
|
|
14
|
+
},
|
|
15
|
+
"author": "",
|
|
16
|
+
"license": "ISC",
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@uxf/core": "10.0.0"
|
|
19
|
+
},
|
|
20
|
+
"peerDependencies": {
|
|
21
|
+
"react": ">=18.0.2"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export type CookiesConsentType = {
|
|
2
|
+
analytics_storage?: boolean;
|
|
3
|
+
ad_storage?: boolean;
|
|
4
|
+
personalization_storage?: boolean;
|
|
5
|
+
};
|
|
6
|
+
export declare const storeConsentToCookie: (consent: CookiesConsentType) => void;
|
|
7
|
+
export declare const readConsentFromCookie: (ctx?: any) => CookiesConsentType;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.readConsentFromCookie = exports.storeConsentToCookie = void 0;
|
|
4
|
+
const cookie_1 = require("@uxf/core/cookie");
|
|
5
|
+
const COOKIE_NAME = "cookieConsent";
|
|
6
|
+
const storeConsentToCookie = (consent) => {
|
|
7
|
+
if (typeof window === "undefined") {
|
|
8
|
+
throw new Error("The 'storeConsentToCookie' method cannot be called on server side.");
|
|
9
|
+
}
|
|
10
|
+
cookie_1.Cookie.create().set(COOKIE_NAME, btoa(JSON.stringify(consent)));
|
|
11
|
+
};
|
|
12
|
+
exports.storeConsentToCookie = storeConsentToCookie;
|
|
13
|
+
const readConsentFromCookie = (ctx) => {
|
|
14
|
+
const cookieConsent = cookie_1.Cookie.create(ctx).get(COOKIE_NAME);
|
|
15
|
+
return JSON.parse(atob(cookieConsent || "") || "{}");
|
|
16
|
+
};
|
|
17
|
+
exports.readConsentFromCookie = readConsentFromCookie;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.GtmScript = void 0;
|
|
7
|
+
const useIsMounted_1 = require("@uxf/core/hooks/useIsMounted");
|
|
8
|
+
const react_1 = __importDefault(require("react"));
|
|
9
|
+
const consent_1 = require("../consent");
|
|
10
|
+
const GtmScript = (props) => {
|
|
11
|
+
const isMounted = (0, useIsMounted_1.useIsMounted)();
|
|
12
|
+
if (!isMounted) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
const decodedCookie = (0, consent_1.readConsentFromCookie)();
|
|
16
|
+
const defaultGtmCookie = `
|
|
17
|
+
window.dataLayer = window.dataLayer || [];
|
|
18
|
+
if (typeof gtag !== "function") { function gtag(){dataLayer.push(arguments)}; }
|
|
19
|
+
gtag("consent", "default", {
|
|
20
|
+
'analytics_storage': '${decodedCookie.analytics_storage ? "granted" : "denied"}',
|
|
21
|
+
'ad_storage': '${decodedCookie.ad_storage ? "granted" : "denied"}',
|
|
22
|
+
'personalization_storage': '${decodedCookie.personalization_storage ? "granted" : "denied"}'}
|
|
23
|
+
);`;
|
|
24
|
+
return (react_1.default.createElement("script", { dangerouslySetInnerHTML: {
|
|
25
|
+
__html: defaultGtmCookie +
|
|
26
|
+
`(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.defer=true;j.src='https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);})(window,document,'script','dataLayer','${props.gtmId}');`,
|
|
27
|
+
} }));
|
|
28
|
+
};
|
|
29
|
+
exports.GtmScript = GtmScript;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type ConsentType = "granted" | "denied";
|
|
2
|
+
export interface GtmConsentData {
|
|
3
|
+
analytics_storage: ConsentType;
|
|
4
|
+
ad_storage: ConsentType;
|
|
5
|
+
personalization_storage: ConsentType;
|
|
6
|
+
}
|
|
7
|
+
export interface GtmDataLayer {
|
|
8
|
+
push: (gtmEventData: GtmConsentData) => void;
|
|
9
|
+
}
|
|
10
|
+
declare global {
|
|
11
|
+
interface Window {
|
|
12
|
+
dataLayer: GtmDataLayer;
|
|
13
|
+
gtag: (command: "consent", event: "default" | "update", params: GtmConsentData) => void;
|
|
14
|
+
}
|
|
15
|
+
}
|
package/src/gtm/types.js
ADDED
package/src/gtm/utils.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.updateGtmConsent = void 0;
|
|
4
|
+
const consent_1 = require("../consent");
|
|
5
|
+
const updateGtmConsent = (consent) => {
|
|
6
|
+
(0, consent_1.storeConsentToCookie)(consent);
|
|
7
|
+
window.dataLayer = window.dataLayer || [];
|
|
8
|
+
if (typeof window.gtag !== "function") {
|
|
9
|
+
window.gtag = function () {
|
|
10
|
+
// eslint-disable-next-line prefer-rest-params
|
|
11
|
+
window.dataLayer.push(arguments);
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
window.gtag("consent", "update", {
|
|
15
|
+
analytics_storage: consent.analytics_storage ? "granted" : "denied",
|
|
16
|
+
ad_storage: consent.ad_storage ? "granted" : "denied",
|
|
17
|
+
personalization_storage: consent.personalization_storage ? "granted" : "denied",
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
exports.updateGtmConsent = updateGtmConsent;
|