@fire-spark/analytics-ga4 0.0.0-stage → 0.1.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.
@@ -0,0 +1,10 @@
1
+ import { AnalyticsAdapter } from "@fire-spark/analytics";
2
+ //#region src/index.d.ts
3
+ type Ga4LogEvent = (name: string, params?: Record<string, unknown>) => Promise<void> | void;
4
+ type Ga4Options = {
5
+ measurementId?: string;
6
+ logEvent?: Ga4LogEvent;
7
+ };
8
+ declare const ga4: (options?: Ga4Options) => AnalyticsAdapter;
9
+ //#endregion
10
+ export { Ga4LogEvent, Ga4Options, ga4 };
package/dist/index.js ADDED
@@ -0,0 +1,65 @@
1
+ //#region src/index.ts
2
+ const host = () => {
3
+ return globalThis;
4
+ };
5
+ const existingGtag = () => {
6
+ const root = host();
7
+ if (typeof root.gtag === "function") return root.gtag;
8
+ const win = globalThis.window;
9
+ if (win && typeof win.gtag === "function") return win.gtag;
10
+ };
11
+ const injectScript = (src, id) => {
12
+ if (typeof document === "undefined") return Promise.resolve();
13
+ if (document.getElementById(id)) return Promise.resolve();
14
+ return new Promise((resolve, reject) => {
15
+ const el = document.createElement("script");
16
+ el.id = id;
17
+ el.async = true;
18
+ el.src = src;
19
+ el.onload = () => resolve();
20
+ el.onerror = () => reject(/* @__PURE__ */ new Error(`Failed to load ${src}`));
21
+ document.head.appendChild(el);
22
+ });
23
+ };
24
+ let loading = null;
25
+ const ensureGtag = async (measurementId) => {
26
+ if (loading) return loading;
27
+ const present = existingGtag();
28
+ if (present) return present;
29
+ if (!measurementId || typeof document === "undefined") return void 0;
30
+ loading = (async () => {
31
+ const root = host();
32
+ root.dataLayer = root.dataLayer ?? [];
33
+ const gtag = (...args) => {
34
+ root.dataLayer?.push(args);
35
+ };
36
+ root.gtag = gtag;
37
+ const src = `https://www.googletagmanager.com/gtag/js?id=${encodeURIComponent(measurementId)}`;
38
+ await injectScript(src, "firespark-ga4-gtag");
39
+ gtag("js", /* @__PURE__ */ new Date());
40
+ gtag("config", measurementId);
41
+ return gtag;
42
+ })();
43
+ try {
44
+ return await loading;
45
+ } finally {
46
+ loading = null;
47
+ }
48
+ };
49
+ const ga4 = (options = {}) => {
50
+ return {
51
+ provider: "ga4",
52
+ send: async (event) => {
53
+ if (event.runtime === "react-native") {
54
+ if (!options.logEvent) return;
55
+ await options.logEvent(event.name, event.params);
56
+ return;
57
+ }
58
+ const gtag = await ensureGtag(options.measurementId);
59
+ if (!gtag) return;
60
+ gtag("event", event.name, event.params);
61
+ }
62
+ };
63
+ };
64
+ //#endregion
65
+ export { ga4 };
package/package.json CHANGED
@@ -1,6 +1,58 @@
1
1
  {
2
2
  "name": "@fire-spark/analytics-ga4",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.2",
4
+ "description": "Fire Spark GA4 analytics adapter (web gtag + RN inject)",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "main": "./dist/index.js",
11
+ "types": "./dist/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/index.js",
16
+ "default": "./dist/index.js"
17
+ }
18
+ },
19
+ "peerDependencies": {
20
+ "@fire-spark/analytics": "^0.1.2"
21
+ },
22
+ "devDependencies": {
23
+ "tsdown": "^0.22.14",
24
+ "typescript": "^7.0.2",
25
+ "vitest": "^2.1.8",
26
+ "@fire-spark/analytics": "0.1.2"
27
+ },
28
+ "publishConfig": {
29
+ "access": "public"
30
+ },
31
+ "tsdown": {
32
+ "entry": [
33
+ "src/index.ts"
34
+ ],
35
+ "format": [
36
+ "esm"
37
+ ],
38
+ "platform": "neutral",
39
+ "dts": {
40
+ "tsconfig": "tsconfig.build.json"
41
+ },
42
+ "clean": true,
43
+ "deps": {
44
+ "neverBundle": [
45
+ "@fire-spark/analytics"
46
+ ]
47
+ }
48
+ },
49
+ "scripts": {
50
+ "build": "tsdown",
51
+ "dev": "tsdown --watch",
52
+ "check-types": "tsc --noEmit",
53
+ "test:utils": "vitest run",
54
+ "lint": "oxlint ./src --max-warnings=0",
55
+ "format": "oxfmt ./src",
56
+ "check-format": "oxfmt --check ./src"
57
+ }
6
58
  }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.