@uxf/analytics 11.10.0 → 11.11.1
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 +22 -11
- package/package.json +1 -1
- package/src/consent/index.d.ts +9 -5
- package/src/consent/index.js +12 -11
- package/src/gtm/types.d.ts +3 -2
- package/src/gtm/use-gtm-script.d.ts +1 -1
- package/src/gtm/use-gtm-script.js +5 -4
- package/src/gtm/utils.d.ts +1 -1
- package/src/gtm/utils.js +6 -5
package/README.md
CHANGED
|
@@ -21,11 +21,15 @@ npm install @uxf/analytics
|
|
|
21
21
|
```ts
|
|
22
22
|
import { storeConsentToCookie } from "@uxf/analytics/consent";
|
|
23
23
|
|
|
24
|
-
storeConsentToCookie(
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
storeConsentToCookie(
|
|
25
|
+
{
|
|
26
|
+
ad_personalization: true,
|
|
27
|
+
ad_storage: false,
|
|
28
|
+
ad_user_data: false,
|
|
29
|
+
analytics_storage: false,
|
|
30
|
+
},
|
|
31
|
+
1 // version - this allows us to simply re-request consents from users
|
|
32
|
+
);
|
|
29
33
|
```
|
|
30
34
|
|
|
31
35
|
### Read consent from cookie
|
|
@@ -43,7 +47,10 @@ This checks if the consent is already stored in cookies.
|
|
|
43
47
|
```tsx
|
|
44
48
|
import { isConsentCookieSet } from "@uxf/analytics/consent";
|
|
45
49
|
|
|
46
|
-
const isSet = isCookieConsentSet(
|
|
50
|
+
const isSet = isCookieConsentSet(
|
|
51
|
+
null,
|
|
52
|
+
1 // version - this allows us to simply re-request consents from users
|
|
53
|
+
); // boolean
|
|
47
54
|
```
|
|
48
55
|
|
|
49
56
|
## GTM
|
|
@@ -68,9 +75,13 @@ This stores the consent to cookie and updates the GTM dataLayer.
|
|
|
68
75
|
```tsx
|
|
69
76
|
import { updateGtmConsent } from "@uxf/analytics/gtm";
|
|
70
77
|
|
|
71
|
-
updateGtmConsent(
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
78
|
+
updateGtmConsent(
|
|
79
|
+
{
|
|
80
|
+
ad_personalization: true,
|
|
81
|
+
ad_storage: false,
|
|
82
|
+
ad_user_data: false,
|
|
83
|
+
analytics_storage: false,
|
|
84
|
+
},
|
|
85
|
+
1
|
|
86
|
+
);
|
|
76
87
|
```
|
package/package.json
CHANGED
package/src/consent/index.d.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
export type CookiesConsentType = {
|
|
2
|
-
|
|
2
|
+
ad_personalization?: boolean;
|
|
3
3
|
ad_storage?: boolean;
|
|
4
|
-
|
|
4
|
+
ad_user_data?: boolean;
|
|
5
|
+
analytics_storage?: boolean;
|
|
6
|
+
};
|
|
7
|
+
export type CookieConsentTypeWithVersion = CookiesConsentType & {
|
|
8
|
+
version: number;
|
|
5
9
|
};
|
|
6
|
-
export declare
|
|
7
|
-
export declare
|
|
8
|
-
export declare
|
|
10
|
+
export declare function storeConsentToCookie(consent: CookiesConsentType, version: number, cookieTtl?: number): void;
|
|
11
|
+
export declare function readConsentFromCookie(ctx?: any): CookieConsentTypeWithVersion;
|
|
12
|
+
export declare function isConsentCookieSet(ctx: any | null, version: number): boolean;
|
package/src/consent/index.js
CHANGED
|
@@ -4,23 +4,24 @@ exports.isConsentCookieSet = exports.readConsentFromCookie = exports.storeConsen
|
|
|
4
4
|
const cookie_1 = require("@uxf/core/cookie");
|
|
5
5
|
const COOKIE_NAME = "cookieConsent";
|
|
6
6
|
const COOKIE_TTL = 3600 * 24 * 90;
|
|
7
|
-
|
|
7
|
+
function storeConsentToCookie(consent, version, cookieTtl = COOKIE_TTL) {
|
|
8
8
|
if (typeof window === "undefined") {
|
|
9
9
|
throw new Error("The 'storeConsentToCookie' method cannot be called on server side.");
|
|
10
10
|
}
|
|
11
|
-
cookie_1.Cookie.create().set(COOKIE_NAME, btoa(JSON.stringify(consent)),
|
|
12
|
-
}
|
|
11
|
+
cookie_1.Cookie.create().set(COOKIE_NAME, btoa(JSON.stringify({ ...consent, version })), cookieTtl);
|
|
12
|
+
}
|
|
13
13
|
exports.storeConsentToCookie = storeConsentToCookie;
|
|
14
|
-
|
|
14
|
+
function readConsentFromCookie(ctx) {
|
|
15
15
|
const cookieConsent = cookie_1.Cookie.create(ctx).get(COOKIE_NAME);
|
|
16
16
|
return JSON.parse(atob(cookieConsent || "") || "{}");
|
|
17
|
-
}
|
|
17
|
+
}
|
|
18
18
|
exports.readConsentFromCookie = readConsentFromCookie;
|
|
19
|
-
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
function isConsentCookieSet(ctx, version) {
|
|
20
|
+
const parsedConsent = readConsentFromCookie(ctx);
|
|
21
|
+
return (typeof parsedConsent.ad_personalization === "boolean" &&
|
|
22
|
+
typeof parsedConsent.ad_storage === "boolean" &&
|
|
23
|
+
typeof parsedConsent.ad_user_data === "boolean" &&
|
|
23
24
|
typeof parsedConsent.analytics_storage === "boolean" &&
|
|
24
|
-
|
|
25
|
-
}
|
|
25
|
+
parsedConsent.version === version);
|
|
26
|
+
}
|
|
26
27
|
exports.isConsentCookieSet = isConsentCookieSet;
|
package/src/gtm/types.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
export type ConsentType = "granted" | "denied";
|
|
2
2
|
export interface GtmConsentData {
|
|
3
|
-
|
|
3
|
+
ad_personalization: ConsentType;
|
|
4
4
|
ad_storage: ConsentType;
|
|
5
|
-
|
|
5
|
+
ad_user_data: ConsentType;
|
|
6
|
+
analytics_storage: ConsentType;
|
|
6
7
|
}
|
|
7
8
|
export interface GtmDataLayer {
|
|
8
9
|
push: (gtmEventData: GtmConsentData) => void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare
|
|
1
|
+
export declare function useGtmScript(gtmId: string): string;
|
|
@@ -2,17 +2,18 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.useGtmScript = void 0;
|
|
4
4
|
const consent_1 = require("../consent");
|
|
5
|
-
|
|
5
|
+
function useGtmScript(gtmId) {
|
|
6
6
|
const decodedCookie = (0, consent_1.readConsentFromCookie)();
|
|
7
7
|
const defaultGtmCookie = `
|
|
8
8
|
window.dataLayer = window.dataLayer || [];
|
|
9
9
|
if (typeof gtag !== "function") { function gtag(){dataLayer.push(arguments)}; }
|
|
10
10
|
gtag("consent", "default", {
|
|
11
|
-
'
|
|
11
|
+
'ad_personalization': '${decodedCookie.ad_personalization ? "granted" : "denied"}',
|
|
12
12
|
'ad_storage': '${decodedCookie.ad_storage ? "granted" : "denied"}',
|
|
13
|
-
'
|
|
13
|
+
'ad_user_data': '${decodedCookie.ad_user_data ? "granted" : "denied"}',
|
|
14
|
+
'analytics_storage': '${decodedCookie.analytics_storage ? "granted" : "denied"}'}
|
|
14
15
|
);`;
|
|
15
16
|
return (defaultGtmCookie +
|
|
16
17
|
`(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','${gtmId}');`);
|
|
17
|
-
}
|
|
18
|
+
}
|
|
18
19
|
exports.useGtmScript = useGtmScript;
|
package/src/gtm/utils.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { CookiesConsentType } from "../consent";
|
|
2
|
-
export declare
|
|
2
|
+
export declare function updateGtmConsent(consent: CookiesConsentType, version: number): void;
|
package/src/gtm/utils.js
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.updateGtmConsent = void 0;
|
|
4
4
|
const consent_1 = require("../consent");
|
|
5
|
-
|
|
6
|
-
(0, consent_1.storeConsentToCookie)(consent);
|
|
5
|
+
function updateGtmConsent(consent, version) {
|
|
6
|
+
(0, consent_1.storeConsentToCookie)(consent, version);
|
|
7
7
|
window.dataLayer = window.dataLayer || [];
|
|
8
8
|
if (typeof window.gtag !== "function") {
|
|
9
9
|
window.gtag = function () {
|
|
@@ -12,9 +12,10 @@ const updateGtmConsent = (consent) => {
|
|
|
12
12
|
};
|
|
13
13
|
}
|
|
14
14
|
window.gtag("consent", "update", {
|
|
15
|
-
|
|
15
|
+
ad_personalization: consent.ad_personalization ? "granted" : "denied",
|
|
16
16
|
ad_storage: consent.ad_storage ? "granted" : "denied",
|
|
17
|
-
|
|
17
|
+
ad_user_data: consent.ad_user_data ? "granted" : "denied",
|
|
18
|
+
analytics_storage: consent.analytics_storage ? "granted" : "denied",
|
|
18
19
|
});
|
|
19
|
-
}
|
|
20
|
+
}
|
|
20
21
|
exports.updateGtmConsent = updateGtmConsent;
|