@whatworks/analytics 1.0.1 → 1.0.2

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.
@@ -2,11 +2,13 @@
2
2
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
3
3
  import { usePathname } from 'next/navigation';
4
4
  import Script from 'next/script';
5
- import { useCallback, useEffect } from 'react';
5
+ import { useCallback, useEffect, useRef } from 'react';
6
6
  import { useCookieBanner } from './CookieBannerProvider.js';
7
7
  export default function FacebookPixel({ pixelId }) {
8
8
  const pathname = usePathname();
9
9
  const { consentStatus, shouldLoadScripts } = useCookieBanner();
10
+ const hasSentInitialRef = useRef(false);
11
+ const lastTrackedPathnameRef = useRef(null);
10
12
  const trackPageView = useCallback(()=>{
11
13
  if (typeof window.fbq === 'function') {
12
14
  window.fbq('track', 'PageView');
@@ -29,13 +31,27 @@ export default function FacebookPixel({ pixelId }) {
29
31
  consentStatus
30
32
  ]);
31
33
  useEffect(()=>{
32
- if (pixelId && consentStatus === 'granted') {
34
+ if (!pixelId || consentStatus !== 'granted') {
35
+ return;
36
+ }
37
+ if (typeof window.fbq !== 'function') {
38
+ return;
39
+ }
40
+ if (!hasSentInitialRef.current) {
33
41
  trackPageView();
42
+ hasSentInitialRef.current = true;
43
+ lastTrackedPathnameRef.current = pathname;
44
+ return;
45
+ }
46
+ if (lastTrackedPathnameRef.current !== pathname) {
47
+ trackPageView();
48
+ lastTrackedPathnameRef.current = pathname;
34
49
  }
35
- // eslint-disable-next-line react-hooks/exhaustive-deps
36
50
  }, [
37
51
  pathname,
38
- consentStatus
52
+ consentStatus,
53
+ pixelId,
54
+ trackPageView
39
55
  ]);
40
56
  if (!pixelId || !shouldLoadScripts) {
41
57
  return null;
@@ -53,14 +69,17 @@ export default function FacebookPixel({ pixelId }) {
53
69
  t.src=v;s=b.getElementsByTagName(e)[0];
54
70
  s.parentNode.insertBefore(t,s)}(window, document,'script',
55
71
  'https://connect.facebook.net/en_US/fbevents.js');
72
+ fbq('consent', '${consentStatus === 'granted' ? 'grant' : 'revoke'}');
56
73
  fbq('init', '${pixelId}');
57
74
  `
58
75
  },
59
76
  id: "fb-pixel",
60
77
  onLoad: ()=>{
61
78
  applyConsent(consentStatus);
62
- if (consentStatus === 'granted') {
79
+ if (consentStatus === 'granted' && !hasSentInitialRef.current) {
63
80
  trackPageView();
81
+ hasSentInitialRef.current = true;
82
+ lastTrackedPathnameRef.current = window.location.pathname;
64
83
  }
65
84
  },
66
85
  strategy: "afterInteractive"
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/FacebookPixel.tsx"],"sourcesContent":["'use client'\nimport { usePathname } from 'next/navigation'\nimport Script from 'next/script'\nimport { useCallback, useEffect } from 'react'\n\nimport { useCookieBanner } from './CookieBannerProvider.js'\n\ninterface FacebookPixelProps {\n pixelId: string\n}\n\nexport default function FacebookPixel({ pixelId }: FacebookPixelProps) {\n const pathname = usePathname()\n const { consentStatus, shouldLoadScripts } = useCookieBanner()\n\n const trackPageView = useCallback(() => {\n if (typeof window.fbq === 'function') {\n window.fbq('track', 'PageView')\n }\n }, [])\n\n const applyConsent = useCallback((status: 'denied' | 'granted') => {\n if (typeof window.fbq !== 'function') {\n return\n }\n\n if (status === 'granted') {\n window.fbq('consent', 'grant')\n } else {\n window.fbq('consent', 'revoke')\n }\n }, [])\n\n useEffect(() => {\n applyConsent(consentStatus)\n }, [applyConsent, consentStatus])\n\n useEffect(() => {\n if (pixelId && consentStatus === 'granted') {\n trackPageView()\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [pathname, consentStatus])\n\n if (!pixelId || !shouldLoadScripts) {\n return null\n }\n\n return (\n <>\n <Script\n dangerouslySetInnerHTML={{\n __html: `\n !function(f,b,e,v,n,t,s)\n {if(f.fbq)return;n=f.fbq=function(){n.callMethod?\n n.callMethod.apply(n,arguments):n.queue.push(arguments)};\n if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';\n n.queue=[];t=b.createElement(e);t.async=!0;\n t.src=v;s=b.getElementsByTagName(e)[0];\n s.parentNode.insertBefore(t,s)}(window, document,'script',\n 'https://connect.facebook.net/en_US/fbevents.js');\n fbq('init', '${pixelId}');\n `,\n }}\n id=\"fb-pixel\"\n onLoad={() => {\n applyConsent(consentStatus)\n if (consentStatus === 'granted') {\n trackPageView()\n }\n }}\n strategy=\"afterInteractive\"\n />\n {consentStatus === 'granted' && (\n <noscript>\n <img\n alt=\"\"\n height=\"1\"\n loading=\"lazy\"\n src={`https://www.facebook.com/tr?id=${pixelId}&ev=PageView&noscript=1`}\n style={{ display: 'none' }}\n width=\"1\"\n />\n </noscript>\n )}\n </>\n )\n}\n\ndeclare global {\n interface Window {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n fbq: (...args: any[]) => void\n }\n}\n"],"names":["usePathname","Script","useCallback","useEffect","useCookieBanner","FacebookPixel","pixelId","pathname","consentStatus","shouldLoadScripts","trackPageView","window","fbq","applyConsent","status","dangerouslySetInnerHTML","__html","id","onLoad","strategy","noscript","img","alt","height","loading","src","style","display","width"],"mappings":"AAAA;;AACA,SAASA,WAAW,QAAQ,kBAAiB;AAC7C,OAAOC,YAAY,cAAa;AAChC,SAASC,WAAW,EAAEC,SAAS,QAAQ,QAAO;AAE9C,SAASC,eAAe,QAAQ,4BAA2B;AAM3D,eAAe,SAASC,cAAc,EAAEC,OAAO,EAAsB;IACnE,MAAMC,WAAWP;IACjB,MAAM,EAAEQ,aAAa,EAAEC,iBAAiB,EAAE,GAAGL;IAE7C,MAAMM,gBAAgBR,YAAY;QAChC,IAAI,OAAOS,OAAOC,GAAG,KAAK,YAAY;YACpCD,OAAOC,GAAG,CAAC,SAAS;QACtB;IACF,GAAG,EAAE;IAEL,MAAMC,eAAeX,YAAY,CAACY;QAChC,IAAI,OAAOH,OAAOC,GAAG,KAAK,YAAY;YACpC;QACF;QAEA,IAAIE,WAAW,WAAW;YACxBH,OAAOC,GAAG,CAAC,WAAW;QACxB,OAAO;YACLD,OAAOC,GAAG,CAAC,WAAW;QACxB;IACF,GAAG,EAAE;IAELT,UAAU;QACRU,aAAaL;IACf,GAAG;QAACK;QAAcL;KAAc;IAEhCL,UAAU;QACR,IAAIG,WAAWE,kBAAkB,WAAW;YAC1CE;QACF;IACA,uDAAuD;IACzD,GAAG;QAACH;QAAUC;KAAc;IAE5B,IAAI,CAACF,WAAW,CAACG,mBAAmB;QAClC,OAAO;IACT;IAEA,qBACE;;0BACE,KAACR;gBACCc,yBAAyB;oBACvBC,QAAQ,CAAC;;;;;;;;;yBASM,EAAEV,QAAQ;UACzB,CAAC;gBACH;gBACAW,IAAG;gBACHC,QAAQ;oBACNL,aAAaL;oBACb,IAAIA,kBAAkB,WAAW;wBAC/BE;oBACF;gBACF;gBACAS,UAAS;;YAEVX,kBAAkB,2BACjB,KAACY;0BACC,cAAA,KAACC;oBACCC,KAAI;oBACJC,QAAO;oBACPC,SAAQ;oBACRC,KAAK,CAAC,+BAA+B,EAAEnB,QAAQ,uBAAuB,CAAC;oBACvEoB,OAAO;wBAAEC,SAAS;oBAAO;oBACzBC,OAAM;;;;;AAMlB"}
1
+ {"version":3,"sources":["../src/FacebookPixel.tsx"],"sourcesContent":["'use client'\nimport { usePathname } from 'next/navigation'\nimport Script from 'next/script'\nimport { useCallback, useEffect, useRef } from 'react'\n\nimport { useCookieBanner } from './CookieBannerProvider.js'\n\ninterface FacebookPixelProps {\n pixelId: string\n}\n\nexport default function FacebookPixel({ pixelId }: FacebookPixelProps) {\n const pathname = usePathname()\n const { consentStatus, shouldLoadScripts } = useCookieBanner()\n const hasSentInitialRef = useRef(false)\n const lastTrackedPathnameRef = useRef<null | string>(null)\n\n const trackPageView = useCallback(() => {\n if (typeof window.fbq === 'function') {\n window.fbq('track', 'PageView')\n }\n }, [])\n\n const applyConsent = useCallback((status: 'denied' | 'granted') => {\n if (typeof window.fbq !== 'function') {\n return\n }\n\n if (status === 'granted') {\n window.fbq('consent', 'grant')\n } else {\n window.fbq('consent', 'revoke')\n }\n }, [])\n\n useEffect(() => {\n applyConsent(consentStatus)\n }, [applyConsent, consentStatus])\n\n useEffect(() => {\n if (!pixelId || consentStatus !== 'granted') {\n return\n }\n if (typeof window.fbq !== 'function') {\n return\n }\n if (!hasSentInitialRef.current) {\n trackPageView()\n hasSentInitialRef.current = true\n lastTrackedPathnameRef.current = pathname\n return\n }\n if (lastTrackedPathnameRef.current !== pathname) {\n trackPageView()\n lastTrackedPathnameRef.current = pathname\n }\n }, [pathname, consentStatus, pixelId, trackPageView])\n\n if (!pixelId || !shouldLoadScripts) {\n return null\n }\n\n return (\n <>\n <Script\n dangerouslySetInnerHTML={{\n __html: `\n !function(f,b,e,v,n,t,s)\n {if(f.fbq)return;n=f.fbq=function(){n.callMethod?\n n.callMethod.apply(n,arguments):n.queue.push(arguments)};\n if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';\n n.queue=[];t=b.createElement(e);t.async=!0;\n t.src=v;s=b.getElementsByTagName(e)[0];\n s.parentNode.insertBefore(t,s)}(window, document,'script',\n 'https://connect.facebook.net/en_US/fbevents.js');\n fbq('consent', '${consentStatus === 'granted' ? 'grant' : 'revoke'}');\n fbq('init', '${pixelId}');\n `,\n }}\n id=\"fb-pixel\"\n onLoad={() => {\n applyConsent(consentStatus)\n if (consentStatus === 'granted' && !hasSentInitialRef.current) {\n trackPageView()\n hasSentInitialRef.current = true\n lastTrackedPathnameRef.current = window.location.pathname\n }\n }}\n strategy=\"afterInteractive\"\n />\n {consentStatus === 'granted' && (\n <noscript>\n <img\n alt=\"\"\n height=\"1\"\n loading=\"lazy\"\n src={`https://www.facebook.com/tr?id=${pixelId}&ev=PageView&noscript=1`}\n style={{ display: 'none' }}\n width=\"1\"\n />\n </noscript>\n )}\n </>\n )\n}\n\ndeclare global {\n interface Window {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n fbq: (...args: any[]) => void\n }\n}\n"],"names":["usePathname","Script","useCallback","useEffect","useRef","useCookieBanner","FacebookPixel","pixelId","pathname","consentStatus","shouldLoadScripts","hasSentInitialRef","lastTrackedPathnameRef","trackPageView","window","fbq","applyConsent","status","current","dangerouslySetInnerHTML","__html","id","onLoad","location","strategy","noscript","img","alt","height","loading","src","style","display","width"],"mappings":"AAAA;;AACA,SAASA,WAAW,QAAQ,kBAAiB;AAC7C,OAAOC,YAAY,cAAa;AAChC,SAASC,WAAW,EAAEC,SAAS,EAAEC,MAAM,QAAQ,QAAO;AAEtD,SAASC,eAAe,QAAQ,4BAA2B;AAM3D,eAAe,SAASC,cAAc,EAAEC,OAAO,EAAsB;IACnE,MAAMC,WAAWR;IACjB,MAAM,EAAES,aAAa,EAAEC,iBAAiB,EAAE,GAAGL;IAC7C,MAAMM,oBAAoBP,OAAO;IACjC,MAAMQ,yBAAyBR,OAAsB;IAErD,MAAMS,gBAAgBX,YAAY;QAChC,IAAI,OAAOY,OAAOC,GAAG,KAAK,YAAY;YACpCD,OAAOC,GAAG,CAAC,SAAS;QACtB;IACF,GAAG,EAAE;IAEL,MAAMC,eAAed,YAAY,CAACe;QAChC,IAAI,OAAOH,OAAOC,GAAG,KAAK,YAAY;YACpC;QACF;QAEA,IAAIE,WAAW,WAAW;YACxBH,OAAOC,GAAG,CAAC,WAAW;QACxB,OAAO;YACLD,OAAOC,GAAG,CAAC,WAAW;QACxB;IACF,GAAG,EAAE;IAELZ,UAAU;QACRa,aAAaP;IACf,GAAG;QAACO;QAAcP;KAAc;IAEhCN,UAAU;QACR,IAAI,CAACI,WAAWE,kBAAkB,WAAW;YAC3C;QACF;QACA,IAAI,OAAOK,OAAOC,GAAG,KAAK,YAAY;YACpC;QACF;QACA,IAAI,CAACJ,kBAAkBO,OAAO,EAAE;YAC9BL;YACAF,kBAAkBO,OAAO,GAAG;YAC5BN,uBAAuBM,OAAO,GAAGV;YACjC;QACF;QACA,IAAII,uBAAuBM,OAAO,KAAKV,UAAU;YAC/CK;YACAD,uBAAuBM,OAAO,GAAGV;QACnC;IACF,GAAG;QAACA;QAAUC;QAAeF;QAASM;KAAc;IAEpD,IAAI,CAACN,WAAW,CAACG,mBAAmB;QAClC,OAAO;IACT;IAEA,qBACE;;0BACE,KAACT;gBACCkB,yBAAyB;oBACvBC,QAAQ,CAAC;;;;;;;;;4BASS,EAAEX,kBAAkB,YAAY,UAAU,SAAS;yBACtD,EAAEF,QAAQ;UACzB,CAAC;gBACH;gBACAc,IAAG;gBACHC,QAAQ;oBACNN,aAAaP;oBACb,IAAIA,kBAAkB,aAAa,CAACE,kBAAkBO,OAAO,EAAE;wBAC7DL;wBACAF,kBAAkBO,OAAO,GAAG;wBAC5BN,uBAAuBM,OAAO,GAAGJ,OAAOS,QAAQ,CAACf,QAAQ;oBAC3D;gBACF;gBACAgB,UAAS;;YAEVf,kBAAkB,2BACjB,KAACgB;0BACC,cAAA,KAACC;oBACCC,KAAI;oBACJC,QAAO;oBACPC,SAAQ;oBACRC,KAAK,CAAC,+BAA+B,EAAEvB,QAAQ,uBAAuB,CAAC;oBACvEwB,OAAO;wBAAEC,SAAS;oBAAO;oBACzBC,OAAM;;;;;AAMlB"}
@@ -2,13 +2,14 @@
2
2
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
3
3
  import { usePathname } from 'next/navigation';
4
4
  import Script from 'next/script';
5
- import { useCallback, useEffect, useRef } from 'react';
5
+ import { useCallback, useEffect, useRef, useState } from 'react';
6
6
  import { useCookieBanner } from './CookieBannerProvider.js';
7
7
  export default function GoogleAnalytics({ gaId }) {
8
8
  const pathname = usePathname();
9
9
  const { consentStatus, shouldLoadScripts } = useCookieBanner();
10
10
  const hasSentInitialRef = useRef(false);
11
11
  const lastTrackedPathnameRef = useRef(null);
12
+ const [isGtagLoaded, setIsGtagLoaded] = useState(false);
12
13
  const trackPageView = useCallback(()=>{
13
14
  if (typeof window.gtag === 'function') {
14
15
  window.gtag('config', gaId, {
@@ -73,7 +74,7 @@ export default function GoogleAnalytics({ gaId }) {
73
74
  updateConsent
74
75
  ]);
75
76
  useEffect(()=>{
76
- if (consentStatus === 'granted' && gaId && typeof window.gtag === 'function') {
77
+ if (isGtagLoaded && consentStatus === 'granted' && gaId && typeof window.gtag === 'function') {
77
78
  if (!hasSentInitialRef.current) {
78
79
  window.gtag('config', gaId, {
79
80
  page_path: window.location.pathname
@@ -84,10 +85,11 @@ export default function GoogleAnalytics({ gaId }) {
84
85
  }
85
86
  }, [
86
87
  consentStatus,
87
- gaId
88
+ gaId,
89
+ isGtagLoaded
88
90
  ]);
89
91
  useEffect(()=>{
90
- if (consentStatus === 'granted' && gaId && typeof window.gtag === 'function') {
92
+ if (isGtagLoaded && consentStatus === 'granted' && gaId && typeof window.gtag === 'function') {
91
93
  if (hasSentInitialRef.current) {
92
94
  if (lastTrackedPathnameRef.current !== pathname) {
93
95
  trackPageView();
@@ -99,7 +101,8 @@ export default function GoogleAnalytics({ gaId }) {
99
101
  pathname,
100
102
  consentStatus,
101
103
  gaId,
102
- trackPageView
104
+ trackPageView,
105
+ isGtagLoaded
103
106
  ]);
104
107
  if (!gaId || !shouldLoadScripts) {
105
108
  return null;
@@ -120,6 +123,9 @@ export default function GoogleAnalytics({ gaId }) {
120
123
  }),
121
124
  /*#__PURE__*/ _jsx(Script, {
122
125
  id: "gtag-base",
126
+ onLoad: ()=>{
127
+ setIsGtagLoaded(true);
128
+ },
123
129
  src: `https://www.googletagmanager.com/gtag/js?id=${gaId}`,
124
130
  strategy: "afterInteractive"
125
131
  }),
@@ -133,18 +139,6 @@ export default function GoogleAnalytics({ gaId }) {
133
139
  `
134
140
  },
135
141
  id: "gtag-init",
136
- onLoad: ()=>{
137
- if (typeof window.gtag === 'function') {
138
- updateConsent(consentStatus);
139
- if (consentStatus === 'granted') {
140
- window.gtag('config', gaId, {
141
- page_path: window.location.pathname
142
- });
143
- hasSentInitialRef.current = true;
144
- lastTrackedPathnameRef.current = window.location.pathname;
145
- }
146
- }
147
- },
148
142
  strategy: "afterInteractive"
149
143
  })
150
144
  ]
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/GoogleAnalytics.tsx"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\n'use client'\n\nimport { usePathname } from 'next/navigation'\nimport Script from 'next/script'\nimport { useCallback, useEffect, useRef } from 'react'\n\nimport { useCookieBanner } from './CookieBannerProvider.js'\n\ninterface GoogleAnalyticsProps {\n gaId: string\n}\n\nexport default function GoogleAnalytics({ gaId }: GoogleAnalyticsProps) {\n const pathname = usePathname()\n const { consentStatus, shouldLoadScripts } = useCookieBanner()\n const hasSentInitialRef = useRef(false)\n const lastTrackedPathnameRef = useRef<null | string>(null)\n\n const trackPageView = useCallback(() => {\n if (typeof window.gtag === 'function') {\n window.gtag('config', gaId, {\n page_path: pathname,\n })\n }\n }, [gaId, pathname])\n\n const updateConsent = useCallback((status: 'denied' | 'granted') => {\n if (typeof window.gtag !== 'function') {\n return\n }\n\n if (status === 'granted') {\n window.gtag('consent', 'update', {\n ad_personalization: 'granted',\n ad_storage: 'granted',\n ad_user_data: 'granted',\n analytics_storage: 'granted',\n functionality_storage: 'granted',\n personalization_storage: 'granted',\n })\n } else {\n window.gtag('consent', 'update', {\n ad_personalization: 'denied',\n ad_storage: 'denied',\n ad_user_data: 'denied',\n analytics_storage: 'denied',\n functionality_storage: 'denied',\n personalization_storage: 'denied',\n security_storage: 'granted',\n })\n }\n }, [])\n\n const getConsentDefaults = useCallback((status: 'denied' | 'granted') => {\n if (status === 'granted') {\n return {\n ad_personalization: 'granted',\n ad_storage: 'granted',\n ad_user_data: 'granted',\n analytics_storage: 'granted',\n functionality_storage: 'granted',\n personalization_storage: 'granted',\n security_storage: 'granted',\n }\n }\n\n return {\n ad_personalization: 'denied',\n ad_storage: 'denied',\n ad_user_data: 'denied',\n analytics_storage: 'denied',\n functionality_storage: 'denied',\n personalization_storage: 'denied',\n security_storage: 'granted',\n }\n }, [])\n\n useEffect(() => {\n updateConsent(consentStatus)\n }, [consentStatus, updateConsent])\n\n useEffect(() => {\n if (consentStatus === 'granted' && gaId && typeof window.gtag === 'function') {\n if (!hasSentInitialRef.current) {\n window.gtag('config', gaId, { page_path: window.location.pathname })\n hasSentInitialRef.current = true\n lastTrackedPathnameRef.current = window.location.pathname\n }\n }\n }, [consentStatus, gaId])\n\n useEffect(() => {\n if (consentStatus === 'granted' && gaId && typeof window.gtag === 'function') {\n if (hasSentInitialRef.current) {\n if (lastTrackedPathnameRef.current !== pathname) {\n trackPageView()\n lastTrackedPathnameRef.current = pathname\n }\n }\n }\n }, [pathname, consentStatus, gaId, trackPageView])\n\n if (!gaId || !shouldLoadScripts) {\n return null\n }\n\n return (\n <>\n <Script\n dangerouslySetInnerHTML={{\n __html: `\n window.dataLayer = window.dataLayer || [];\n function gtag(){dataLayer.push(arguments);}\n window.gtag = window.gtag || gtag;\n gtag('consent', 'default', ${JSON.stringify(getConsentDefaults(consentStatus))});\n `,\n }}\n id=\"ga-consent-default\"\n strategy=\"beforeInteractive\"\n />\n <Script\n id=\"gtag-base\"\n src={`https://www.googletagmanager.com/gtag/js?id=${gaId}`}\n strategy=\"afterInteractive\"\n />\n <Script\n dangerouslySetInnerHTML={{\n __html: `\n window.dataLayer = window.dataLayer || [];\n function gtag(){dataLayer.push(arguments);} \n window.gtag = gtag;\n gtag('js', new Date());\n `,\n }}\n id=\"gtag-init\"\n onLoad={() => {\n if (typeof window.gtag === 'function') {\n updateConsent(consentStatus)\n if (consentStatus === 'granted') {\n window.gtag('config', gaId, { page_path: window.location.pathname })\n hasSentInitialRef.current = true\n lastTrackedPathnameRef.current = window.location.pathname\n }\n }\n }}\n strategy=\"afterInteractive\"\n />\n </>\n )\n}\n\ndeclare global {\n interface Window {\n dataLayer: any[]\n gtag: (...args: any[]) => void\n }\n}\n"],"names":["usePathname","Script","useCallback","useEffect","useRef","useCookieBanner","GoogleAnalytics","gaId","pathname","consentStatus","shouldLoadScripts","hasSentInitialRef","lastTrackedPathnameRef","trackPageView","window","gtag","page_path","updateConsent","status","ad_personalization","ad_storage","ad_user_data","analytics_storage","functionality_storage","personalization_storage","security_storage","getConsentDefaults","current","location","dangerouslySetInnerHTML","__html","JSON","stringify","id","strategy","src","onLoad"],"mappings":"AAAA,qDAAqD,GACrD;;AAEA,SAASA,WAAW,QAAQ,kBAAiB;AAC7C,OAAOC,YAAY,cAAa;AAChC,SAASC,WAAW,EAAEC,SAAS,EAAEC,MAAM,QAAQ,QAAO;AAEtD,SAASC,eAAe,QAAQ,4BAA2B;AAM3D,eAAe,SAASC,gBAAgB,EAAEC,IAAI,EAAwB;IACpE,MAAMC,WAAWR;IACjB,MAAM,EAAES,aAAa,EAAEC,iBAAiB,EAAE,GAAGL;IAC7C,MAAMM,oBAAoBP,OAAO;IACjC,MAAMQ,yBAAyBR,OAAsB;IAErD,MAAMS,gBAAgBX,YAAY;QAChC,IAAI,OAAOY,OAAOC,IAAI,KAAK,YAAY;YACrCD,OAAOC,IAAI,CAAC,UAAUR,MAAM;gBAC1BS,WAAWR;YACb;QACF;IACF,GAAG;QAACD;QAAMC;KAAS;IAEnB,MAAMS,gBAAgBf,YAAY,CAACgB;QACjC,IAAI,OAAOJ,OAAOC,IAAI,KAAK,YAAY;YACrC;QACF;QAEA,IAAIG,WAAW,WAAW;YACxBJ,OAAOC,IAAI,CAAC,WAAW,UAAU;gBAC/BI,oBAAoB;gBACpBC,YAAY;gBACZC,cAAc;gBACdC,mBAAmB;gBACnBC,uBAAuB;gBACvBC,yBAAyB;YAC3B;QACF,OAAO;YACLV,OAAOC,IAAI,CAAC,WAAW,UAAU;gBAC/BI,oBAAoB;gBACpBC,YAAY;gBACZC,cAAc;gBACdC,mBAAmB;gBACnBC,uBAAuB;gBACvBC,yBAAyB;gBACzBC,kBAAkB;YACpB;QACF;IACF,GAAG,EAAE;IAEL,MAAMC,qBAAqBxB,YAAY,CAACgB;QACtC,IAAIA,WAAW,WAAW;YACxB,OAAO;gBACLC,oBAAoB;gBACpBC,YAAY;gBACZC,cAAc;gBACdC,mBA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oBACvBC,QAAQ,CAAC;;;;;UAKT,CAAC;gBACH;gBACAG,IAAG;gBACHG,QAAQ;oBACN,IAAI,OAAOtB,OAAOC,IAAI,KAAK,YAAY;wBACrCE,cAAcR;wBACd,IAAIA,kBAAkB,WAAW;4BAC/BK,OAAOC,IAAI,CAAC,UAAUR,MAAM;gCAAES,WAAWF,OAAOc,QAAQ,CAACpB,QAAQ;4BAAC;4BAClEG,kBAAkBgB,OAAO,GAAG;4BAC5Bf,uBAAuBe,OAAO,GAAGb,OAAOc,QAAQ,CAACpB,QAAQ;wBAC3D;oBACF;gBACF;gBACA0B,UAAS;;;;AAIjB"}
1
+ {"version":3,"sources":["../src/GoogleAnalytics.tsx"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\n'use client'\n\nimport { usePathname } from 'next/navigation'\nimport Script from 'next/script'\nimport { useCallback, useEffect, useRef, useState } from 'react'\n\nimport { useCookieBanner } from './CookieBannerProvider.js'\n\ninterface GoogleAnalyticsProps {\n gaId: string\n}\n\nexport default function GoogleAnalytics({ gaId }: GoogleAnalyticsProps) {\n const pathname = usePathname()\n const { consentStatus, shouldLoadScripts } = useCookieBanner()\n const hasSentInitialRef = useRef(false)\n const lastTrackedPathnameRef = useRef<null | string>(null)\n const [isGtagLoaded, setIsGtagLoaded] = useState(false)\n\n const trackPageView = useCallback(() => {\n if (typeof window.gtag === 'function') {\n window.gtag('config', gaId, {\n page_path: pathname,\n })\n }\n }, [gaId, pathname])\n\n const updateConsent = useCallback((status: 'denied' | 'granted') => {\n if (typeof window.gtag !== 'function') {\n return\n }\n\n if (status === 'granted') {\n window.gtag('consent', 'update', {\n ad_personalization: 'granted',\n ad_storage: 'granted',\n ad_user_data: 'granted',\n analytics_storage: 'granted',\n functionality_storage: 'granted',\n personalization_storage: 'granted',\n })\n } else {\n window.gtag('consent', 'update', {\n ad_personalization: 'denied',\n ad_storage: 'denied',\n ad_user_data: 'denied',\n analytics_storage: 'denied',\n functionality_storage: 'denied',\n personalization_storage: 'denied',\n security_storage: 'granted',\n })\n }\n }, [])\n\n const getConsentDefaults = useCallback((status: 'denied' | 'granted') => {\n if (status === 'granted') {\n return {\n ad_personalization: 'granted',\n ad_storage: 'granted',\n ad_user_data: 'granted',\n analytics_storage: 'granted',\n functionality_storage: 'granted',\n personalization_storage: 'granted',\n security_storage: 'granted',\n }\n }\n\n return {\n ad_personalization: 'denied',\n ad_storage: 'denied',\n ad_user_data: 'denied',\n analytics_storage: 'denied',\n functionality_storage: 'denied',\n personalization_storage: 'denied',\n security_storage: 'granted',\n }\n }, [])\n\n useEffect(() => {\n updateConsent(consentStatus)\n }, [consentStatus, updateConsent])\n\n useEffect(() => {\n if (isGtagLoaded && consentStatus === 'granted' && gaId && typeof window.gtag === 'function') {\n if (!hasSentInitialRef.current) {\n window.gtag('config', gaId, { page_path: window.location.pathname })\n hasSentInitialRef.current = true\n lastTrackedPathnameRef.current = window.location.pathname\n }\n }\n }, [consentStatus, gaId, isGtagLoaded])\n\n useEffect(() => {\n if (isGtagLoaded && consentStatus === 'granted' && gaId && typeof window.gtag === 'function') {\n if (hasSentInitialRef.current) {\n if (lastTrackedPathnameRef.current !== pathname) {\n trackPageView()\n lastTrackedPathnameRef.current = pathname\n }\n }\n }\n }, [pathname, consentStatus, gaId, trackPageView, isGtagLoaded])\n\n if (!gaId || !shouldLoadScripts) {\n return null\n }\n\n return (\n <>\n <Script\n dangerouslySetInnerHTML={{\n __html: `\n window.dataLayer = window.dataLayer || [];\n function gtag(){dataLayer.push(arguments);}\n window.gtag = window.gtag || gtag;\n gtag('consent', 'default', ${JSON.stringify(getConsentDefaults(consentStatus))});\n `,\n }}\n id=\"ga-consent-default\"\n strategy=\"beforeInteractive\"\n />\n <Script\n id=\"gtag-base\"\n onLoad={() => {\n setIsGtagLoaded(true)\n }}\n src={`https://www.googletagmanager.com/gtag/js?id=${gaId}`}\n strategy=\"afterInteractive\"\n />\n <Script\n dangerouslySetInnerHTML={{\n __html: `\n window.dataLayer = window.dataLayer || [];\n function gtag(){dataLayer.push(arguments);} \n window.gtag = gtag;\n gtag('js', new Date());\n `,\n }}\n id=\"gtag-init\"\n strategy=\"afterInteractive\"\n />\n </>\n )\n}\n\ndeclare global {\n interface Window {\n dataLayer: any[]\n gtag: (...args: any[]) => void\n }\n}\n"],"names":["usePathname","Script","useCallback","useEffect","useRef","useState","useCookieBanner","GoogleAnalytics","gaId","pathname","consentStatus","shouldLoadScripts","hasSentInitialRef","lastTrackedPathnameRef","isGtagLoaded","setIsGtagLoaded","trackPageView","window","gtag","page_path","updateConsent","status","ad_personalization","ad_storage","ad_user_data","analytics_storage","functionality_storage","personalization_storage","security_storage","getConsentDefaults","current","location","dangerouslySetInnerHTML","__html","JSON","stringify","id","strategy","onLoad","src"],"mappings":"AAAA,qDAAqD,GACrD;;AAEA,SAASA,WAAW,QAAQ,kBAAiB;AAC7C,OAAOC,YAAY,cAAa;AAChC,SAASC,WAAW,EAAEC,SAAS,EAAEC,MAAM,EAAEC,QAAQ,QAAQ,QAAO;AAEhE,SAASC,eAAe,QAAQ,4BAA2B;AAM3D,eAAe,SAASC,gBAAgB,EAAEC,IAAI,EAAwB;IACpE,MAAMC,WAAWT;IACjB,MAAM,EAAEU,aAAa,EAAEC,iBAAiB,EAAE,GAAGL;IAC7C,MAAMM,oBAAoBR,OAAO;IACjC,MAAMS,yBAAyBT,OAAsB;IACrD,MAAM,CAACU,cAAcC,gBAAgB,GAAGV,SAAS;IAEjD,MAAMW,gBAAgBd,YAAY;QAChC,IAAI,OAAOe,OAAOC,IAAI,KAAK,YAAY;YACrCD,OAAOC,IAAI,CAAC,UAAUV,MAAM;gBAC1BW,WAAWV;YACb;QACF;IACF,GAAG;QAACD;QAAMC;KAAS;IAEnB,MAAMW,gBAAgBlB,YAAY,CAACmB;QACjC,IAAI,OAAOJ,OAAOC,IAAI,KAAK,YAAY;YACrC;QACF;QAEA,IAAIG,WAAW,WAAW;YACxBJ,OAAOC,IAAI,CAAC,WAAW,UAAU;gBA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AC/CO;oBACAH,uBAAuBiB,OAAO,GAAGrB;gBACnC;YACF;QACF;IACF,GAAG;QAACA;QAAUC;QAAeF;QAAMQ;QAAeF;KAAa;IAE/D,IAAI,CAACN,QAAQ,CAACG,mBAAmB;QAC/B,OAAO;IACT;IAEA,qBACE;;0BACE,KAACV;gBACC+B,yBAAyB;oBACvBC,QAAQ,CAAC;;;;uCAIoB,EAAEC,KAAKC,SAAS,CAACN,mBAAmBnB,gBAAgB;UACjF,CAAC;gBACH;gBACA0B,IAAG;gBACHC,UAAS;;0BAEX,KAACpC;gBACCmC,IAAG;gBACHE,QAAQ;oBACNvB,gBAAgB;gBAClB;gBACAwB,KAAK,CAAC,4CAA4C,EAAE/B,MAAM;gBAC1D6B,UAAS;;0BAEX,KAACpC;gBACC+B,yBAAyB;oBACvBC,QAAQ,CAAC;;;;;UAKT,CAAC;gBACH;gBACAG,IAAG;gBACHC,UAAS;;;;AAIjB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whatworks/analytics",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Analytics components for Next.js with cookie consent.",
5
5
  "license": "MIT",
6
6
  "type": "module",