@codeswayam/analytics 0.1.0 → 0.1.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.
@@ -0,0 +1,23 @@
1
+ export interface AnalyticsConfig {
2
+ gtmId?: string;
3
+ ga4Id?: string;
4
+ metaPixelId?: string;
5
+ gscVerification?: string;
6
+ hotjarId?: string;
7
+ clarityId?: string;
8
+ }
9
+ interface AnalyticsProps extends AnalyticsConfig {
10
+ appName?: string;
11
+ }
12
+ export declare function Analytics({ gtmId, ga4Id, metaPixelId, gscVerification, hotjarId, clarityId, appName, }: AnalyticsProps): import("react/jsx-runtime").JSX.Element | null;
13
+ export declare function trackEvent(eventName: string, eventData?: Record<string, any>): void;
14
+ export declare function trackPageView(url: string): void;
15
+ declare global {
16
+ interface Window {
17
+ dataLayer: any[];
18
+ fbq: any;
19
+ gtag: (...args: any[]) => void;
20
+ }
21
+ }
22
+ export {};
23
+ //# sourceMappingURL=Analytics.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Analytics.d.ts","sourceRoot":"","sources":["../src/Analytics.tsx"],"names":[],"mappings":"AAIA,MAAM,WAAW,eAAe;IAC9B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,UAAU,cAAe,SAAQ,eAAe;IAC9C,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,wBAAgB,SAAS,CAAC,EACxB,KAAK,EACL,KAAK,EACL,WAAW,EACX,eAAe,EACf,QAAQ,EACR,SAAS,EACT,OAAO,GACR,EAAE,cAAc,kDAwEhB;AAED,wBAAgB,UAAU,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,QAI5E;AAED,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,QAIxC;AAED,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,MAAM;QACd,SAAS,EAAE,GAAG,EAAE,CAAC;QACjB,GAAG,EAAE,GAAG,CAAC;QACT,IAAI,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;KAChC;CACF"}
@@ -0,0 +1,41 @@
1
+ 'use client';
2
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import Script from 'next/script';
4
+ export function Analytics({ gtmId, ga4Id, metaPixelId, gscVerification, hotjarId, clarityId, appName, }) {
5
+ if (!gtmId && !ga4Id && !metaPixelId && !hotjarId && !clarityId)
6
+ return null;
7
+ return (_jsxs(_Fragment, { children: [gscVerification && (_jsx("meta", { name: "google-site-verification", content: gscVerification })), gtmId && (_jsxs(_Fragment, { children: [_jsx(Script, { id: "gtm-script", strategy: "afterInteractive", children: `(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
8
+ new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
9
+ j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
10
+ 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
11
+ })(window,document,'script','dataLayer','${gtmId}');
12
+ ${appName ? `window.dataLayer=window.dataLayer||[];window.dataLayer.push({'appName':'${appName}'});` : ''}` }), _jsx("noscript", { children: _jsx("iframe", { src: `https://www.googletagmanager.com/ns.html?id=${gtmId}`, height: "0", width: "0", style: { display: 'none', visibility: 'hidden' } }) })] })), ga4Id && !gtmId && (_jsxs(_Fragment, { children: [_jsx(Script, { src: `https://www.googletagmanager.com/gtag/js?id=${ga4Id}`, strategy: "afterInteractive" }), _jsx(Script, { id: "ga4-script", strategy: "afterInteractive", children: `window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments);}
13
+ gtag('js',new Date());gtag('config','${ga4Id}',{'send_page_view':true});` })] })), metaPixelId && (_jsxs(_Fragment, { children: [_jsx(Script, { id: "meta-pixel", strategy: "afterInteractive", children: `!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
14
+ n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
15
+ n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
16
+ t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
17
+ document,'script','https://connect.facebook.net/en_US/fbevents.js');
18
+ fbq('init','${metaPixelId}');fbq('track','PageView');` }), _jsx("noscript", { children: _jsx("img", { height: "1", width: "1", style: { display: 'none' }, src: `https://www.facebook.com/tr?id=${metaPixelId}&ev=PageView&noscript=1`, alt: "" }) })] })), hotjarId && (_jsx(Script, { id: "hotjar-script", strategy: "afterInteractive", children: `(function(h,o,t,j,a,r){h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)};
19
+ h._hjSettings={hjid:${hotjarId},hjsv:6};a=o.getElementsByTagName('head')[0];
20
+ r=o.createElement('script');r.async=1;r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv;
21
+ a.appendChild(r);})(window,document,'https://static.hotjar.com/c/hotjar-','.js?sv=');` })), clarityId && (_jsx(Script, { id: "clarity-script", strategy: "afterInteractive", children: `(function(c,l,a,r,i,t,y){c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};
22
+ t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;
23
+ y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);
24
+ })(window,document,"clarity","script","${clarityId}");` }))] }));
25
+ }
26
+ export function trackEvent(eventName, eventData) {
27
+ if (typeof window === 'undefined')
28
+ return;
29
+ if (window.dataLayer)
30
+ window.dataLayer.push(Object.assign({ event: eventName }, eventData));
31
+ if (window.fbq)
32
+ window.fbq('trackCustom', eventName, eventData);
33
+ }
34
+ export function trackPageView(url) {
35
+ if (typeof window === 'undefined')
36
+ return;
37
+ if (window.dataLayer)
38
+ window.dataLayer.push({ event: 'page_view', page_path: url });
39
+ if (window.fbq)
40
+ window.fbq('track', 'PageView');
41
+ }
@@ -1,3 +1,4 @@
1
1
  export { Analytics, trackEvent, trackPageView } from './Analytics';
2
2
  export type { AnalyticsConfig } from './Analytics';
3
3
  export { getAnalyticsConfig } from './server';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACnE,YAAY,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { Analytics, trackEvent, trackPageView } from './Analytics';
2
+ export { getAnalyticsConfig } from './server';
@@ -0,0 +1,11 @@
1
+ import type { AnalyticsConfig } from './Analytics';
2
+ /**
3
+ * Fetch analytics config for a given appId from the core-api.
4
+ * Call this in your Next.js root layout (server component).
5
+ *
6
+ * Usage:
7
+ * const config = await getAnalyticsConfig('web');
8
+ * <Analytics {...config} appName="web" />
9
+ */
10
+ export declare function getAnalyticsConfig(appId: string): Promise<AnalyticsConfig>;
11
+ //# sourceMappingURL=server.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAEnD;;;;;;;GAOG;AACH,wBAAsB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC,CAWhF"}
package/dist/server.js ADDED
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Fetch analytics config for a given appId from the core-api.
3
+ * Call this in your Next.js root layout (server component).
4
+ *
5
+ * Usage:
6
+ * const config = await getAnalyticsConfig('web');
7
+ * <Analytics {...config} appName="web" />
8
+ */
9
+ export async function getAnalyticsConfig(appId) {
10
+ const apiUrl = process.env.NEXT_PUBLIC_API_URL || process.env.API_URL || 'http://localhost:3000';
11
+ try {
12
+ const res = await fetch(`${apiUrl}/admin/analytics/config/${appId}`, {
13
+ next: { revalidate: 300 },
14
+ });
15
+ if (!res.ok)
16
+ return {};
17
+ return res.json();
18
+ }
19
+ catch (_a) {
20
+ return {};
21
+ }
22
+ }
package/package.json CHANGED
@@ -1,12 +1,25 @@
1
1
  {
2
2
  "name": "@codeswayam/analytics",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Centralized analytics and tracking for CodeSwayam apps",
5
- "main": "./src/index.tsx",
6
- "types": "./src/index.tsx",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "files": [
9
+ "dist",
10
+ "README.md"
11
+ ],
12
+ "exports": {
13
+ ".": {
14
+ "import": "./dist/index.js",
15
+ "require": "./dist/index.js",
16
+ "types": "./dist/index.d.ts"
17
+ }
18
+ },
7
19
  "scripts": {
8
20
  "build": "tsc",
9
- "dev": "tsc --watch"
21
+ "dev": "tsc --watch",
22
+ "prepublishOnly": "npm run build"
10
23
  },
11
24
  "dependencies": {
12
25
  "react": "^19.2.3",
package/src/Analytics.tsx DELETED
@@ -1,118 +0,0 @@
1
- 'use client';
2
-
3
- import Script from 'next/script';
4
-
5
- export interface AnalyticsConfig {
6
- gtmId?: string;
7
- ga4Id?: string;
8
- metaPixelId?: string;
9
- gscVerification?: string;
10
- hotjarId?: string;
11
- clarityId?: string;
12
- }
13
-
14
- interface AnalyticsProps extends AnalyticsConfig {
15
- appName?: string;
16
- }
17
-
18
- export function Analytics({
19
- gtmId,
20
- ga4Id,
21
- metaPixelId,
22
- gscVerification,
23
- hotjarId,
24
- clarityId,
25
- appName,
26
- }: AnalyticsProps) {
27
- if (!gtmId && !ga4Id && !metaPixelId && !hotjarId && !clarityId) return null;
28
-
29
- return (
30
- <>
31
- {gscVerification && (
32
- <meta name="google-site-verification" content={gscVerification} />
33
- )}
34
-
35
- {gtmId && (
36
- <>
37
- <Script id="gtm-script" strategy="afterInteractive">
38
- {`(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
39
- new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
40
- j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
41
- 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
42
- })(window,document,'script','dataLayer','${gtmId}');
43
- ${appName ? `window.dataLayer=window.dataLayer||[];window.dataLayer.push({'appName':'${appName}'});` : ''}`}
44
- </Script>
45
- <noscript>
46
- <iframe src={`https://www.googletagmanager.com/ns.html?id=${gtmId}`}
47
- height="0" width="0" style={{ display: 'none', visibility: 'hidden' }} />
48
- </noscript>
49
- </>
50
- )}
51
-
52
- {ga4Id && !gtmId && (
53
- <>
54
- <Script src={`https://www.googletagmanager.com/gtag/js?id=${ga4Id}`} strategy="afterInteractive" />
55
- <Script id="ga4-script" strategy="afterInteractive">
56
- {`window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments);}
57
- gtag('js',new Date());gtag('config','${ga4Id}',{'send_page_view':true});`}
58
- </Script>
59
- </>
60
- )}
61
-
62
- {metaPixelId && (
63
- <>
64
- <Script id="meta-pixel" strategy="afterInteractive">
65
- {`!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
66
- n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
67
- n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
68
- t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
69
- document,'script','https://connect.facebook.net/en_US/fbevents.js');
70
- fbq('init','${metaPixelId}');fbq('track','PageView');`}
71
- </Script>
72
- <noscript>
73
- <img height="1" width="1" style={{ display: 'none' }}
74
- src={`https://www.facebook.com/tr?id=${metaPixelId}&ev=PageView&noscript=1`} alt="" />
75
- </noscript>
76
- </>
77
- )}
78
-
79
- {hotjarId && (
80
- <Script id="hotjar-script" strategy="afterInteractive">
81
- {`(function(h,o,t,j,a,r){h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)};
82
- h._hjSettings={hjid:${hotjarId},hjsv:6};a=o.getElementsByTagName('head')[0];
83
- r=o.createElement('script');r.async=1;r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv;
84
- a.appendChild(r);})(window,document,'https://static.hotjar.com/c/hotjar-','.js?sv=');`}
85
- </Script>
86
- )}
87
-
88
- {clarityId && (
89
- <Script id="clarity-script" strategy="afterInteractive">
90
- {`(function(c,l,a,r,i,t,y){c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};
91
- t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;
92
- y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);
93
- })(window,document,"clarity","script","${clarityId}");`}
94
- </Script>
95
- )}
96
- </>
97
- );
98
- }
99
-
100
- export function trackEvent(eventName: string, eventData?: Record<string, any>) {
101
- if (typeof window === 'undefined') return;
102
- if (window.dataLayer) window.dataLayer.push({ event: eventName, ...eventData });
103
- if (window.fbq) window.fbq('trackCustom', eventName, eventData);
104
- }
105
-
106
- export function trackPageView(url: string) {
107
- if (typeof window === 'undefined') return;
108
- if (window.dataLayer) window.dataLayer.push({ event: 'page_view', page_path: url });
109
- if (window.fbq) window.fbq('track', 'PageView');
110
- }
111
-
112
- declare global {
113
- interface Window {
114
- dataLayer: any[];
115
- fbq: any;
116
- gtag: (...args: any[]) => void;
117
- }
118
- }
package/src/server.ts DELETED
@@ -1,22 +0,0 @@
1
- import type { AnalyticsConfig } from './Analytics';
2
-
3
- /**
4
- * Fetch analytics config for a given appId from the core-api.
5
- * Call this in your Next.js root layout (server component).
6
- *
7
- * Usage:
8
- * const config = await getAnalyticsConfig('web');
9
- * <Analytics {...config} appName="web" />
10
- */
11
- export async function getAnalyticsConfig(appId: string): Promise<AnalyticsConfig> {
12
- const apiUrl = process.env.NEXT_PUBLIC_API_URL || process.env.API_URL || 'http://localhost:3000';
13
- try {
14
- const res = await fetch(`${apiUrl}/admin/analytics/config/${appId}`, {
15
- next: { revalidate: 300 },
16
- } as RequestInit);
17
- if (!res.ok) return {};
18
- return res.json();
19
- } catch {
20
- return {};
21
- }
22
- }
package/tsconfig.json DELETED
@@ -1,18 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2017",
4
- "lib": ["dom", "dom.iterable", "esnext"],
5
- "allowJs": true,
6
- "skipLibCheck": true,
7
- "strict": true,
8
- "noEmit": true,
9
- "esModuleInterop": true,
10
- "module": "esnext",
11
- "moduleResolution": "bundler",
12
- "resolveJsonModule": true,
13
- "isolatedModules": true,
14
- "jsx": "react-jsx"
15
- },
16
- "include": ["src/**/*"],
17
- "exclude": ["node_modules"]
18
- }