@shware/analytics 11.13.0 → 11.13.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.
- package/README.md +14 -1
- package/dist/hooks/use-app-analytics.cjs +3 -2
- package/dist/hooks/use-app-analytics.cjs.map +1 -1
- package/dist/hooks/use-app-analytics.d.cts.map +1 -1
- package/dist/hooks/use-app-analytics.d.mts.map +1 -1
- package/dist/hooks/use-app-analytics.mjs +4 -3
- package/dist/hooks/use-app-analytics.mjs.map +1 -1
- package/dist/hooks/use-web-analytics.cjs +5 -5
- package/dist/hooks/use-web-analytics.cjs.map +1 -1
- package/dist/hooks/use-web-analytics.d.cts.map +1 -1
- package/dist/hooks/use-web-analytics.d.mts.map +1 -1
- package/dist/hooks/use-web-analytics.mjs +5 -5
- package/dist/hooks/use-web-analytics.mjs.map +1 -1
- package/dist/native/deep-link.cjs +1 -2
- package/dist/native/deep-link.cjs.map +1 -1
- package/dist/native/deep-link.d.cts.map +1 -1
- package/dist/native/deep-link.d.mts.map +1 -1
- package/dist/native/deep-link.mjs +0 -1
- package/dist/native/deep-link.mjs.map +1 -1
- package/dist/native/setup.cjs +1 -2
- package/dist/native/setup.cjs.map +1 -1
- package/dist/native/setup.d.cts.map +1 -1
- package/dist/native/setup.d.mts.map +1 -1
- package/dist/native/setup.mjs +0 -1
- package/dist/native/setup.mjs.map +1 -1
- package/package.json +5 -6
- package/dist/hooks/use-previous.cjs +0 -14
- package/dist/hooks/use-previous.cjs.map +0 -1
- package/dist/hooks/use-previous.d.cts +0 -4
- package/dist/hooks/use-previous.d.cts.map +0 -1
- package/dist/hooks/use-previous.d.mts +0 -4
- package/dist/hooks/use-previous.d.mts.map +0 -1
- package/dist/hooks/use-previous.mjs +0 -13
- package/dist/hooks/use-previous.mjs.map +0 -1
package/README.md
CHANGED
|
@@ -80,9 +80,9 @@ setupAnalytics({
|
|
|
80
80
|
React Native (Expo):
|
|
81
81
|
|
|
82
82
|
```ts
|
|
83
|
-
import { Platform } from 'react-native';
|
|
84
83
|
import { setupAnalytics } from '@shware/analytics';
|
|
85
84
|
import { deepLink, getDeviceId, getTags, storage } from '@shware/analytics/native';
|
|
85
|
+
import { Platform } from 'react-native';
|
|
86
86
|
|
|
87
87
|
setupAnalytics({
|
|
88
88
|
storage,
|
|
@@ -158,6 +158,19 @@ and the ad click ids) are ignored when comparing, so a landing page that cleans
|
|
|
158
158
|
URL with `replaceState` does not count itself twice. `page_load_id` rotates on exactly the same
|
|
159
159
|
changes. The framework `Analytics` components wire this up.
|
|
160
160
|
|
|
161
|
+
Mount one analytics reporter per navigation stream. `previous_page_path` and
|
|
162
|
+
`previous_screen_class` identify the preceding view queued by that reporter: for
|
|
163
|
+
`A → B → C → back to B`, the predecessors are `undefined`, `A`, `B`, and `C`. History advances
|
|
164
|
+
when the view is queued, without waiting for delivery, and survives Activity effect cleanup.
|
|
165
|
+
Restoring B emits another view with B as its predecessor and settles engagement time, even
|
|
166
|
+
when React Compiler reuses the page element without rendering it again. Development Strict
|
|
167
|
+
Mode effect replay also queues another view, so its predecessor is the first view itself.
|
|
168
|
+
|
|
169
|
+
`page_referrer` describes the navigation source instead: the document's initial
|
|
170
|
+
`document.referrer`, then the preceding page URL after an in-app navigation. Restoring the same
|
|
171
|
+
page preserves that navigation referrer and `page_load_id`. Unrelated renders and uncommitted
|
|
172
|
+
navigation do not advance the reporter's view history.
|
|
173
|
+
|
|
161
174
|
## Backend API
|
|
162
175
|
|
|
163
176
|
- /analytics/tracks: track events
|
|
@@ -3,7 +3,6 @@ const require_setup_index = require("../setup/index.cjs");
|
|
|
3
3
|
const require_constants_storage = require("../constants/storage.cjs");
|
|
4
4
|
const require_setup_session = require("../setup/session.cjs");
|
|
5
5
|
const require_track_index = require("../track/index.cjs");
|
|
6
|
-
const require_hooks_use_previous = require("./use-previous.cjs");
|
|
7
6
|
let react = require("react");
|
|
8
7
|
let react_native = require("react-native");
|
|
9
8
|
//#region src/hooks/use-app-analytics.ts
|
|
@@ -24,7 +23,7 @@ function sendUserEngagement() {
|
|
|
24
23
|
});
|
|
25
24
|
}
|
|
26
25
|
function useAppAnalytics(pathname) {
|
|
27
|
-
const
|
|
26
|
+
const lastScreenPath = (0, react.useRef)(void 0);
|
|
28
27
|
(0, react.useEffect)(() => {
|
|
29
28
|
const session = require_setup_session.getSession();
|
|
30
29
|
sendFirstOpen(pathname);
|
|
@@ -40,12 +39,14 @@ function useAppAnalytics(pathname) {
|
|
|
40
39
|
return () => subscription.remove();
|
|
41
40
|
}, []);
|
|
42
41
|
(0, react.useEffect)(() => {
|
|
42
|
+
const prevPathname = lastScreenPath.current;
|
|
43
43
|
require_track_index.track("screen_view", {
|
|
44
44
|
screen_name: pathname,
|
|
45
45
|
screen_class: pathname,
|
|
46
46
|
previous_screen_class: prevPathname ?? void 0,
|
|
47
47
|
engagement_time_msec: prevPathname ? require_setup_session.getSession().flush() : void 0
|
|
48
48
|
});
|
|
49
|
+
lastScreenPath.current = pathname;
|
|
49
50
|
}, [pathname]);
|
|
50
51
|
}
|
|
51
52
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-app-analytics.cjs","names":["config","keys","getSession","
|
|
1
|
+
{"version":3,"file":"use-app-analytics.cjs","names":["config","keys","getSession","useRef","AppState"],"sources":["../../src/hooks/use-app-analytics.ts"],"sourcesContent":["import { useEffect, useRef } from 'react';\nimport { AppState } from 'react-native';\nimport { keys } from '../constants/storage';\nimport { config } from '../setup/index';\nimport { getSession } from '../setup/session';\nimport { sendPendingEvents, track } from '../track/index';\n\nfunction sendFirstOpen(pathname: string) {\n if (config.storage.getItem(keys.first_open_time)) return;\n track('first_open', { screen_name: pathname, screen_class: pathname });\n config.storage.setItem(keys.first_open_time, new Date().toISOString());\n}\n\nfunction sendUserEngagement() {\n const engagement_time_msec = getSession().flush();\n if (engagement_time_msec <= 0) return;\n track('user_engagement', { engagement_time_msec, trigger: 'background' });\n}\n\nexport function useAppAnalytics(pathname: string) {\n const lastScreenPath = useRef<string | undefined>(undefined);\n\n useEffect(() => {\n const session = getSession();\n\n sendFirstOpen(pathname);\n\n const subscription = AppState.addEventListener('change', (state) => {\n session.updateAccumulator();\n // when returning to the foreground from the background\n if (state === 'active' && !session.isActive()) {\n session.updateActive(true);\n }\n // when entering the background\n else if (state !== 'active' && session.isActive()) {\n session.updateActive(false);\n sendUserEngagement();\n // The app may be suspended or killed soon after going to the background: send the queue\n // now rather than after the batch delay — a new session's session_start may be in it.\n // React Native has no beacon, so this is the usual send, started at once.\n sendPendingEvents();\n }\n });\n\n return () => subscription.remove();\n }, []);\n\n // when the screen is switched, the engagement time of the previous screen is recorded\n useEffect(() => {\n const prevPathname = lastScreenPath.current;\n track('screen_view', {\n screen_name: pathname,\n screen_class: pathname,\n previous_screen_class: prevPathname ?? undefined,\n engagement_time_msec: prevPathname ? getSession().flush() : undefined,\n });\n // Keep the history across Activity effect cleanup and restoration.\n lastScreenPath.current = pathname;\n }, [pathname]);\n}\n"],"mappings":";;;;;;;;AAOA,SAAS,cAAc,UAAkB;CACvC,IAAIA,oBAAAA,OAAO,QAAQ,QAAQC,0BAAAA,KAAK,eAAe,GAAG;CAClD,oBAAA,MAAM,cAAc;EAAE,aAAa;EAAU,cAAc;CAAS,CAAC;CACrE,oBAAA,OAAO,QAAQ,QAAQA,0BAAAA,KAAK,kCAAiB,IAAI,KAAK,EAAA,CAAE,YAAY,CAAC;AACvE;AAEA,SAAS,qBAAqB;CAC5B,MAAM,uBAAuBC,sBAAAA,WAAW,CAAC,CAAC,MAAM;CAChD,IAAI,wBAAwB,GAAG;CAC/B,oBAAA,MAAM,mBAAmB;EAAE;EAAsB,SAAS;CAAa,CAAC;AAC1E;AAEA,SAAgB,gBAAgB,UAAkB;CAChD,MAAM,kBAAA,GAAiBC,MAAAA,OAAAA,CAA2B,KAAA,CAAS;CAE3D,CAAA,GAAA,MAAA,UAAA,OAAgB;EACd,MAAM,UAAUD,sBAAAA,WAAW;EAE3B,cAAc,QAAQ;EAEtB,MAAM,eAAeE,aAAAA,SAAS,iBAAiB,WAAW,UAAU;GAClE,QAAQ,kBAAkB;GAE1B,IAAI,UAAU,YAAY,CAAC,QAAQ,SAAS,GAC1C,QAAQ,aAAa,IAAI;QAGtB,IAAI,UAAU,YAAY,QAAQ,SAAS,GAAG;IACjD,QAAQ,aAAa,KAAK;IAC1B,mBAAmB;IAInB,oBAAA,kBAAkB;GACpB;EACF,CAAC;EAED,aAAa,aAAa,OAAO;CACnC,GAAG,CAAC,CAAC;CAGL,CAAA,GAAA,MAAA,UAAA,OAAgB;EACd,MAAM,eAAe,eAAe;EACpC,oBAAA,MAAM,eAAe;GACnB,aAAa;GACb,cAAc;GACd,uBAAuB,gBAAgB,KAAA;GACvC,sBAAsB,eAAeF,sBAAAA,WAAW,CAAC,CAAC,MAAM,IAAI,KAAA;EAC9D,CAAC;EAED,eAAe,UAAU;CAC3B,GAAG,CAAC,QAAQ,CAAC;AACf"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-app-analytics.d.cts","names":[],"sources":["../../src/hooks/use-app-analytics.ts"],"mappings":";
|
|
1
|
+
{"version":3,"file":"use-app-analytics.d.cts","names":[],"sources":["../../src/hooks/use-app-analytics.ts"],"mappings":";wBAmBgB,gBAAgB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-app-analytics.d.mts","names":[],"sources":["../../src/hooks/use-app-analytics.ts"],"mappings":";
|
|
1
|
+
{"version":3,"file":"use-app-analytics.d.mts","names":[],"sources":["../../src/hooks/use-app-analytics.ts"],"mappings":";wBAmBgB,gBAAgB"}
|
|
@@ -2,8 +2,7 @@ import { config } from "../setup/index.mjs";
|
|
|
2
2
|
import { keys } from "../constants/storage.mjs";
|
|
3
3
|
import { getSession } from "../setup/session.mjs";
|
|
4
4
|
import { sendPendingEvents, track } from "../track/index.mjs";
|
|
5
|
-
import {
|
|
6
|
-
import { useEffect } from "react";
|
|
5
|
+
import { useEffect, useRef } from "react";
|
|
7
6
|
import { AppState } from "react-native";
|
|
8
7
|
//#region src/hooks/use-app-analytics.ts
|
|
9
8
|
function sendFirstOpen(pathname) {
|
|
@@ -23,7 +22,7 @@ function sendUserEngagement() {
|
|
|
23
22
|
});
|
|
24
23
|
}
|
|
25
24
|
function useAppAnalytics(pathname) {
|
|
26
|
-
const
|
|
25
|
+
const lastScreenPath = useRef(void 0);
|
|
27
26
|
useEffect(() => {
|
|
28
27
|
const session = getSession();
|
|
29
28
|
sendFirstOpen(pathname);
|
|
@@ -39,12 +38,14 @@ function useAppAnalytics(pathname) {
|
|
|
39
38
|
return () => subscription.remove();
|
|
40
39
|
}, []);
|
|
41
40
|
useEffect(() => {
|
|
41
|
+
const prevPathname = lastScreenPath.current;
|
|
42
42
|
track("screen_view", {
|
|
43
43
|
screen_name: pathname,
|
|
44
44
|
screen_class: pathname,
|
|
45
45
|
previous_screen_class: prevPathname ?? void 0,
|
|
46
46
|
engagement_time_msec: prevPathname ? getSession().flush() : void 0
|
|
47
47
|
});
|
|
48
|
+
lastScreenPath.current = pathname;
|
|
48
49
|
}, [pathname]);
|
|
49
50
|
}
|
|
50
51
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-app-analytics.mjs","names":[],"sources":["../../src/hooks/use-app-analytics.ts"],"sourcesContent":["import { useEffect } from 'react';\nimport { AppState } from 'react-native';\nimport { keys } from '../constants/storage';\nimport { config } from '../setup/index';\nimport { getSession } from '../setup/session';\nimport { sendPendingEvents, track } from '../track/index';\
|
|
1
|
+
{"version":3,"file":"use-app-analytics.mjs","names":[],"sources":["../../src/hooks/use-app-analytics.ts"],"sourcesContent":["import { useEffect, useRef } from 'react';\nimport { AppState } from 'react-native';\nimport { keys } from '../constants/storage';\nimport { config } from '../setup/index';\nimport { getSession } from '../setup/session';\nimport { sendPendingEvents, track } from '../track/index';\n\nfunction sendFirstOpen(pathname: string) {\n if (config.storage.getItem(keys.first_open_time)) return;\n track('first_open', { screen_name: pathname, screen_class: pathname });\n config.storage.setItem(keys.first_open_time, new Date().toISOString());\n}\n\nfunction sendUserEngagement() {\n const engagement_time_msec = getSession().flush();\n if (engagement_time_msec <= 0) return;\n track('user_engagement', { engagement_time_msec, trigger: 'background' });\n}\n\nexport function useAppAnalytics(pathname: string) {\n const lastScreenPath = useRef<string | undefined>(undefined);\n\n useEffect(() => {\n const session = getSession();\n\n sendFirstOpen(pathname);\n\n const subscription = AppState.addEventListener('change', (state) => {\n session.updateAccumulator();\n // when returning to the foreground from the background\n if (state === 'active' && !session.isActive()) {\n session.updateActive(true);\n }\n // when entering the background\n else if (state !== 'active' && session.isActive()) {\n session.updateActive(false);\n sendUserEngagement();\n // The app may be suspended or killed soon after going to the background: send the queue\n // now rather than after the batch delay — a new session's session_start may be in it.\n // React Native has no beacon, so this is the usual send, started at once.\n sendPendingEvents();\n }\n });\n\n return () => subscription.remove();\n }, []);\n\n // when the screen is switched, the engagement time of the previous screen is recorded\n useEffect(() => {\n const prevPathname = lastScreenPath.current;\n track('screen_view', {\n screen_name: pathname,\n screen_class: pathname,\n previous_screen_class: prevPathname ?? undefined,\n engagement_time_msec: prevPathname ? getSession().flush() : undefined,\n });\n // Keep the history across Activity effect cleanup and restoration.\n lastScreenPath.current = pathname;\n }, [pathname]);\n}\n"],"mappings":";;;;;;;AAOA,SAAS,cAAc,UAAkB;CACvC,IAAI,OAAO,QAAQ,QAAQ,KAAK,eAAe,GAAG;CAClD,MAAM,cAAc;EAAE,aAAa;EAAU,cAAc;CAAS,CAAC;CACrE,OAAO,QAAQ,QAAQ,KAAK,kCAAiB,IAAI,KAAK,EAAA,CAAE,YAAY,CAAC;AACvE;AAEA,SAAS,qBAAqB;CAC5B,MAAM,uBAAuB,WAAW,CAAC,CAAC,MAAM;CAChD,IAAI,wBAAwB,GAAG;CAC/B,MAAM,mBAAmB;EAAE;EAAsB,SAAS;CAAa,CAAC;AAC1E;AAEA,SAAgB,gBAAgB,UAAkB;CAChD,MAAM,iBAAiB,OAA2B,KAAA,CAAS;CAE3D,gBAAgB;EACd,MAAM,UAAU,WAAW;EAE3B,cAAc,QAAQ;EAEtB,MAAM,eAAe,SAAS,iBAAiB,WAAW,UAAU;GAClE,QAAQ,kBAAkB;GAE1B,IAAI,UAAU,YAAY,CAAC,QAAQ,SAAS,GAC1C,QAAQ,aAAa,IAAI;QAGtB,IAAI,UAAU,YAAY,QAAQ,SAAS,GAAG;IACjD,QAAQ,aAAa,KAAK;IAC1B,mBAAmB;IAInB,kBAAkB;GACpB;EACF,CAAC;EAED,aAAa,aAAa,OAAO;CACnC,GAAG,CAAC,CAAC;CAGL,gBAAgB;EACd,MAAM,eAAe,eAAe;EACpC,MAAM,eAAe;GACnB,aAAa;GACb,cAAc;GACd,uBAAuB,gBAAgB,KAAA;GACvC,sBAAsB,eAAe,WAAW,CAAC,CAAC,MAAM,IAAI,KAAA;EAC9D,CAAC;EAED,eAAe,UAAU;CAC3B,GAAG,CAAC,QAAQ,CAAC;AACf"}
|
|
@@ -3,7 +3,6 @@ const require_setup_index = require("../setup/index.cjs");
|
|
|
3
3
|
const require_constants_storage = require("../constants/storage.cjs");
|
|
4
4
|
const require_setup_session = require("../setup/session.cjs");
|
|
5
5
|
const require_track_index = require("../track/index.cjs");
|
|
6
|
-
const require_hooks_use_previous = require("./use-previous.cjs");
|
|
7
6
|
const require_web_page_key = require("../web/page-key.cjs");
|
|
8
7
|
const require_web_index = require("../web/index.cjs");
|
|
9
8
|
let _shware_utils = require("@shware/utils");
|
|
@@ -63,8 +62,7 @@ function onVisibilityChange() {
|
|
|
63
62
|
*/
|
|
64
63
|
function useWebAnalytics(pathname, search) {
|
|
65
64
|
const page = require_web_page_key.getPageKey(pathname, search);
|
|
66
|
-
const
|
|
67
|
-
const prevPathname = require_hooks_use_previous.usePrevious(pathname);
|
|
65
|
+
const lastViewPath = (0, react.useRef)(void 0);
|
|
68
66
|
const hasSendScroll = (0, react.useRef)(false);
|
|
69
67
|
(0, react.useEffect)(() => {
|
|
70
68
|
hasSendScroll.current = false;
|
|
@@ -109,14 +107,16 @@ function useWebAnalytics(pathname, search) {
|
|
|
109
107
|
};
|
|
110
108
|
}, []);
|
|
111
109
|
(0, react.useEffect)(() => {
|
|
110
|
+
const prevPathname = lastViewPath.current;
|
|
112
111
|
require_track_index.track("page_view", {
|
|
113
112
|
page_path: pathname,
|
|
114
113
|
page_title: document.title,
|
|
115
114
|
page_referrer: require_web_index.getPageReferrer(),
|
|
116
115
|
page_location: window.location.href,
|
|
117
|
-
previous_page_path:
|
|
118
|
-
engagement_time_msec:
|
|
116
|
+
previous_page_path: prevPathname,
|
|
117
|
+
engagement_time_msec: prevPathname === void 0 ? void 0 : require_setup_session.getSession().flush()
|
|
119
118
|
});
|
|
119
|
+
lastViewPath.current = pathname;
|
|
120
120
|
}, [page]);
|
|
121
121
|
}
|
|
122
122
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-web-analytics.cjs","names":["config","keys","getPageReferrer","getSession","getPageKey","
|
|
1
|
+
{"version":3,"file":"use-web-analytics.cjs","names":["config","keys","getPageReferrer","getSession","getPageKey","useRef","throttle"],"sources":["../../src/hooks/use-web-analytics.ts"],"sourcesContent":["import { throttle } from '@shware/utils';\nimport { useEffect, useRef } from 'react';\nimport { keys } from '../constants/storage';\nimport { config } from '../setup/index';\nimport { getSession } from '../setup/session';\nimport { sendBeacon, sendPendingEvents, track } from '../track/index';\nimport { getPageReferrer } from '../web/index';\nimport { getPageKey } from '../web/page-key';\n\nfunction sendFirstVisit(pathname: string) {\n if (config.storage.getItem(keys.first_visit_time)) return;\n track('first_visit', {\n page_path: pathname,\n page_title: document.title,\n page_referrer: getPageReferrer(),\n page_location: window.location.href,\n });\n config.storage.setItem(keys.first_visit_time, new Date().toISOString());\n}\n\nfunction sendUserEngagement(trigger: 'pagehide' | 'visibilitychange') {\n const engagement_time_msec = getSession().flush();\n if (engagement_time_msec <= 0) return;\n sendBeacon('user_engagement', { engagement_time_msec, trigger });\n}\n\n/** Whether the event was actually sent — a zero-engagement crossing reports nothing. */\nfunction sendScroll(): boolean {\n const engagement_time_msec = getSession().flush();\n if (engagement_time_msec <= 0) return false;\n track('scroll', { engagement_time_msec });\n return true;\n}\n\nfunction getScrollPercent() {\n const scrollTop = window.scrollY || document.documentElement.scrollTop;\n const windowHeight = window.innerHeight;\n const docHeight = document.documentElement.scrollHeight;\n if (docHeight === 0) return 0;\n return ((scrollTop + windowHeight) * 100) / docHeight;\n}\n\n// The queue goes first: a new session's `session_start` is in it, ahead of the engagement that\n// reports for that session. On `hidden` as well as on `pagehide`, as GA4 does: a mobile browser\n// often discards a hidden page without firing `pagehide`.\nfunction onPageHide() {\n getSession().pagehide();\n sendPendingEvents();\n sendUserEngagement('pagehide');\n}\n\nfunction onVisibilityChange() {\n getSession().visibilitychange(document.visibilityState);\n if (document.visibilityState === 'hidden') {\n sendPendingEvents();\n sendUserEngagement('visibilitychange');\n }\n}\n\n/**\n * 1. send session_start event when the page is loaded\n * 2. send scroll event when the user scrolls more than 90% of the page\n * 3. send user_engagement event when the page is hidden or the user is not focused\n * 4. send page_view when the page changes — path or query, see `getPageKey`; `search` is the\n * router's query string (`?a=1`, or `''`), required so no caller forgets that the query is\n * part of what makes a page\n */\nexport function useWebAnalytics(pathname: string, search: string) {\n const page = getPageKey(pathname, search);\n const lastViewPath = useRef<string | undefined>(undefined);\n\n // reset state when the page changes and send scroll when the user navigates to a new page\n const hasSendScroll = useRef(false);\n useEffect(() => {\n hasSendScroll.current = false;\n }, [page]);\n\n useEffect(() => {\n // One lookup for the whole effect: `addEventListener` and its matching\n // `removeEventListener` have to be handed the very same function.\n const session = getSession();\n\n sendFirstVisit(pathname);\n\n const onScroll = throttle(() => {\n session.updateAccumulator();\n if (hasSendScroll.current) return;\n // only send scroll when the user has scrolled more than 90% of the page\n if (getScrollPercent() < 90) return;\n // A crossing with no engaged time (a restored scroll position in an unfocused window) must\n // not consume the page's one shot: the flag is set only once the event is actually sent.\n hasSendScroll.current = sendScroll();\n }, 500);\n\n const checkpointEvents = ['mousedown', 'keydown', 'touchstart'];\n const checkpoint = throttle(session.updateAccumulator, 1000);\n\n window.addEventListener('focus', session.focus);\n window.addEventListener('blur', session.blur);\n window.addEventListener('scroll', onScroll, { passive: true });\n window.addEventListener('pageshow', session.pageshow);\n window.addEventListener('pagehide', onPageHide);\n document.addEventListener('visibilitychange', onVisibilityChange);\n\n // save checkpoint\n checkpointEvents.forEach((e) => {\n window.addEventListener(e, checkpoint, { passive: true, capture: true });\n });\n\n return () => {\n window.removeEventListener('focus', session.focus);\n window.removeEventListener('blur', session.blur);\n window.removeEventListener('scroll', onScroll);\n window.removeEventListener('pageshow', session.pageshow);\n window.removeEventListener('pagehide', onPageHide);\n document.removeEventListener('visibilitychange', onVisibilityChange);\n checkpointEvents.forEach((e) => window.removeEventListener(e, checkpoint, { capture: true }));\n\n onScroll.cancel();\n checkpoint.cancel();\n };\n }, []);\n\n useEffect(() => {\n // `page_path` stays the path — the query is in `page_location` — and the previous page is\n // named by its path too, so a query-only change reports the same path as its predecessor.\n const prevPathname = lastViewPath.current;\n track('page_view', {\n page_path: pathname,\n page_title: document.title,\n page_referrer: getPageReferrer(),\n page_location: window.location.href,\n previous_page_path: prevPathname,\n engagement_time_msec: prevPathname === undefined ? undefined : getSession().flush(),\n });\n // Advance only after this view is queued; effect restoration also emits a new view.\n lastViewPath.current = pathname;\n }, [page]);\n}\n"],"mappings":";;;;;;;;;;AASA,SAAS,eAAe,UAAkB;CACxC,IAAIA,oBAAAA,OAAO,QAAQ,QAAQC,0BAAAA,KAAK,gBAAgB,GAAG;CACnD,oBAAA,MAAM,eAAe;EACnB,WAAW;EACX,YAAY,SAAS;EACrB,eAAeC,kBAAAA,gBAAgB;EAC/B,eAAe,OAAO,SAAS;CACjC,CAAC;CACD,oBAAA,OAAO,QAAQ,QAAQD,0BAAAA,KAAK,mCAAkB,IAAI,KAAK,EAAA,CAAE,YAAY,CAAC;AACxE;A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WAAW,CAAC,CAAC,MAAM;EACpF,CAAC;EAED,aAAa,UAAU;CACzB,GAAG,CAAC,IAAI,CAAC;AACX"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-web-analytics.d.cts","names":[],"sources":["../../src/hooks/use-web-analytics.ts"],"mappings":";;;;;;;;;
|
|
1
|
+
{"version":3,"file":"use-web-analytics.d.cts","names":[],"sources":["../../src/hooks/use-web-analytics.ts"],"mappings":";;;;;;;;;wBAmEgB,gBAAgB,kBAAkB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-web-analytics.d.mts","names":[],"sources":["../../src/hooks/use-web-analytics.ts"],"mappings":";;;;;;;;;
|
|
1
|
+
{"version":3,"file":"use-web-analytics.d.mts","names":[],"sources":["../../src/hooks/use-web-analytics.ts"],"mappings":";;;;;;;;;wBAmEgB,gBAAgB,kBAAkB"}
|
|
@@ -2,7 +2,6 @@ import { config } from "../setup/index.mjs";
|
|
|
2
2
|
import { keys } from "../constants/storage.mjs";
|
|
3
3
|
import { getSession } from "../setup/session.mjs";
|
|
4
4
|
import { sendBeacon, sendPendingEvents, track } from "../track/index.mjs";
|
|
5
|
-
import { usePrevious } from "./use-previous.mjs";
|
|
6
5
|
import { getPageKey } from "../web/page-key.mjs";
|
|
7
6
|
import { getPageReferrer } from "../web/index.mjs";
|
|
8
7
|
import { throttle } from "@shware/utils";
|
|
@@ -62,8 +61,7 @@ function onVisibilityChange() {
|
|
|
62
61
|
*/
|
|
63
62
|
function useWebAnalytics(pathname, search) {
|
|
64
63
|
const page = getPageKey(pathname, search);
|
|
65
|
-
const
|
|
66
|
-
const prevPathname = usePrevious(pathname);
|
|
64
|
+
const lastViewPath = useRef(void 0);
|
|
67
65
|
const hasSendScroll = useRef(false);
|
|
68
66
|
useEffect(() => {
|
|
69
67
|
hasSendScroll.current = false;
|
|
@@ -108,14 +106,16 @@ function useWebAnalytics(pathname, search) {
|
|
|
108
106
|
};
|
|
109
107
|
}, []);
|
|
110
108
|
useEffect(() => {
|
|
109
|
+
const prevPathname = lastViewPath.current;
|
|
111
110
|
track("page_view", {
|
|
112
111
|
page_path: pathname,
|
|
113
112
|
page_title: document.title,
|
|
114
113
|
page_referrer: getPageReferrer(),
|
|
115
114
|
page_location: window.location.href,
|
|
116
|
-
previous_page_path:
|
|
117
|
-
engagement_time_msec:
|
|
115
|
+
previous_page_path: prevPathname,
|
|
116
|
+
engagement_time_msec: prevPathname === void 0 ? void 0 : getSession().flush()
|
|
118
117
|
});
|
|
118
|
+
lastViewPath.current = pathname;
|
|
119
119
|
}, [page]);
|
|
120
120
|
}
|
|
121
121
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-web-analytics.mjs","names":[],"sources":["../../src/hooks/use-web-analytics.ts"],"sourcesContent":["import { throttle } from '@shware/utils';\nimport { useEffect, useRef } from 'react';\nimport { keys } from '../constants/storage';\nimport { config } from '../setup/index';\nimport { getSession } from '../setup/session';\nimport { sendBeacon, sendPendingEvents, track } from '../track/index';\nimport { getPageReferrer } from '../web/index';\nimport { getPageKey } from '../web/page-key';\
|
|
1
|
+
{"version":3,"file":"use-web-analytics.mjs","names":[],"sources":["../../src/hooks/use-web-analytics.ts"],"sourcesContent":["import { throttle } from '@shware/utils';\nimport { useEffect, useRef } from 'react';\nimport { keys } from '../constants/storage';\nimport { config } from '../setup/index';\nimport { getSession } from '../setup/session';\nimport { sendBeacon, sendPendingEvents, track } from '../track/index';\nimport { getPageReferrer } from '../web/index';\nimport { getPageKey } from '../web/page-key';\n\nfunction sendFirstVisit(pathname: string) {\n if (config.storage.getItem(keys.first_visit_time)) return;\n track('first_visit', {\n page_path: pathname,\n page_title: document.title,\n page_referrer: getPageReferrer(),\n page_location: window.location.href,\n });\n config.storage.setItem(keys.first_visit_time, new Date().toISOString());\n}\n\nfunction sendUserEngagement(trigger: 'pagehide' | 'visibilitychange') {\n const engagement_time_msec = getSession().flush();\n if (engagement_time_msec <= 0) return;\n sendBeacon('user_engagement', { engagement_time_msec, trigger });\n}\n\n/** Whether the event was actually sent — a zero-engagement crossing reports nothing. */\nfunction sendScroll(): boolean {\n const engagement_time_msec = getSession().flush();\n if (engagement_time_msec <= 0) return false;\n track('scroll', { engagement_time_msec });\n return true;\n}\n\nfunction getScrollPercent() {\n const scrollTop = window.scrollY || document.documentElement.scrollTop;\n const windowHeight = window.innerHeight;\n const docHeight = document.documentElement.scrollHeight;\n if (docHeight === 0) return 0;\n return ((scrollTop + windowHeight) * 100) / docHeight;\n}\n\n// The queue goes first: a new session's `session_start` is in it, ahead of the engagement that\n// reports for that session. On `hidden` as well as on `pagehide`, as GA4 does: a mobile browser\n// often discards a hidden page without firing `pagehide`.\nfunction onPageHide() {\n getSession().pagehide();\n sendPendingEvents();\n sendUserEngagement('pagehide');\n}\n\nfunction onVisibilityChange() {\n getSession().visibilitychange(document.visibilityState);\n if (document.visibilityState === 'hidden') {\n sendPendingEvents();\n sendUserEngagement('visibilitychange');\n }\n}\n\n/**\n * 1. send session_start event when the page is loaded\n * 2. send scroll event when the user scrolls more than 90% of the page\n * 3. send user_engagement event when the page is hidden or the user is not focused\n * 4. send page_view when the page changes — path or query, see `getPageKey`; `search` is the\n * router's query string (`?a=1`, or `''`), required so no caller forgets that the query is\n * part of what makes a page\n */\nexport function useWebAnalytics(pathname: string, search: string) {\n const page = getPageKey(pathname, search);\n const lastViewPath = useRef<string | undefined>(undefined);\n\n // reset state when the page changes and send scroll when the user navigates to a new page\n const hasSendScroll = useRef(false);\n useEffect(() => {\n hasSendScroll.current = false;\n }, [page]);\n\n useEffect(() => {\n // One lookup for the whole effect: `addEventListener` and its matching\n // `removeEventListener` have to be handed the very same function.\n const session = getSession();\n\n sendFirstVisit(pathname);\n\n const onScroll = throttle(() => {\n session.updateAccumulator();\n if (hasSendScroll.current) return;\n // only send scroll when the user has scrolled more than 90% of the page\n if (getScrollPercent() < 90) return;\n // A crossing with no engaged time (a restored scroll position in an unfocused window) must\n // not consume the page's one shot: the flag is set only once the event is actually sent.\n hasSendScroll.current = sendScroll();\n }, 500);\n\n const checkpointEvents = ['mousedown', 'keydown', 'touchstart'];\n const checkpoint = throttle(session.updateAccumulator, 1000);\n\n window.addEventListener('focus', session.focus);\n window.addEventListener('blur', session.blur);\n window.addEventListener('scroll', onScroll, { passive: true });\n window.addEventListener('pageshow', session.pageshow);\n window.addEventListener('pagehide', onPageHide);\n document.addEventListener('visibilitychange', onVisibilityChange);\n\n // save checkpoint\n checkpointEvents.forEach((e) => {\n window.addEventListener(e, checkpoint, { passive: true, capture: true });\n });\n\n return () => {\n window.removeEventListener('focus', session.focus);\n window.removeEventListener('blur', session.blur);\n window.removeEventListener('scroll', onScroll);\n window.removeEventListener('pageshow', session.pageshow);\n window.removeEventListener('pagehide', onPageHide);\n document.removeEventListener('visibilitychange', onVisibilityChange);\n checkpointEvents.forEach((e) => window.removeEventListener(e, checkpoint, { capture: true }));\n\n onScroll.cancel();\n checkpoint.cancel();\n };\n }, []);\n\n useEffect(() => {\n // `page_path` stays the path — the query is in `page_location` — and the previous page is\n // named by its path too, so a query-only change reports the same path as its predecessor.\n const prevPathname = lastViewPath.current;\n track('page_view', {\n page_path: pathname,\n page_title: document.title,\n page_referrer: getPageReferrer(),\n page_location: window.location.href,\n previous_page_path: prevPathname,\n engagement_time_msec: prevPathname === undefined ? undefined : getSession().flush(),\n });\n // Advance only after this view is queued; effect restoration also emits a new view.\n lastViewPath.current = pathname;\n }, [page]);\n}\n"],"mappings":";;;;;;;;;AASA,SAAS,eAAe,UAAkB;CACxC,IAAI,OAAO,QAAQ,QAAQ,KAAK,gBAAgB,GAAG;CACnD,MAAM,eAAe;EACnB,WAAW;EACX,YAAY,SAAS;EACrB,eAAe,gBAAgB;EAC/B,eAAe,OAAO,SAAS;CACjC,CAAC;CACD,OAAO,QAAQ,QAAQ,KAAK,mCAAkB,IAAI,KAAK,EAAA,CAAE,YAAY,CAAC;AACxE;AAEA,SAAS,mBAAmB,SAA0C;C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D,OAAO,iBAAiB,SAAS,QAAQ,KAAK;EAC9C,OAAO,iBAAiB,QAAQ,QAAQ,IAAI;EAC5C,OAAO,iBAAiB,UAAU,UAAU,EAAE,SAAS,KAAK,CAAC;EAC7D,OAAO,iBAAiB,YAAY,QAAQ,QAAQ;EACpD,OAAO,iBAAiB,YAAY,UAAU;EAC9C,SAAS,iBAAiB,oBAAoB,kBAAkB;EAGhE,iBAAiB,SAAS,MAAM;GAC9B,OAAO,iBAAiB,GAAG,YAAY;IAAE,SAAS;IAAM,SAAS;GAAK,CAAC;EACzE,CAAC;EAED,aAAa;GACX,OAAO,oBAAoB,SAAS,QAAQ,KAAK;GACjD,OAAO,oBAAoB,QAAQ,QAAQ,IAAI;GAC/C,OAAO,oBAAoB,UAAU,QAAQ;GAC7C,OAAO,oBAAoB,YAAY,QAAQ,QAAQ;GACvD,OAAO,oBAAoB,YAAY,UAAU;GACjD,SAAS,oBAAoB,oBAAoB,kBAAkB;GACnE,iBAAiB,SAAS,MAAM,OAAO,oBAAoB,GAAG,YAAY,EAAE,SAAS,KAAK,CAAC,CAAC;GAE5F,SAAS,OAAO;GAChB,WAAW,OAAO;EACpB;CACF,GAAG,CAAC,CAAC;CAEL,gBAAgB;EAGd,MAAM,eAAe,aAAa;EAClC,MAAM,aAAa;GACjB,WAAW;GACX,YAAY,SAAS;GACrB,eAAe,gBAAgB;GAC/B,eAAe,OAAO,SAAS;GAC/B,oBAAoB;GACpB,sBAAsB,iBAAiB,KAAA,IAAY,KAAA,IAAY,WAAW,CAAC,CAAC,MAAM;EACpF,CAAC;EAED,aAAa,UAAU;CACzB,GAAG,CAAC,IAAI,CAAC;AACX"}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
let react_native = require("react-native");
|
|
3
|
-
let react_native_url_polyfill = require("react-native-url-polyfill");
|
|
4
3
|
//#region src/native/deep-link.ts
|
|
5
4
|
/**
|
|
6
5
|
* The link that opened the app for this visit, the app's landing page: `getTags` merges its URL
|
|
@@ -57,7 +56,7 @@ const deepLink = {
|
|
|
57
56
|
await resolveInitialUrl();
|
|
58
57
|
if (!openUrl) return {};
|
|
59
58
|
const at = openUrl.indexOf("?");
|
|
60
|
-
const params = new
|
|
59
|
+
const params = new URLSearchParams(at === -1 ? "" : openUrl.slice(at + 1).split("#")[0]);
|
|
61
60
|
const tags = { page_location: openUrl };
|
|
62
61
|
for (const key of UTM_KEYS) {
|
|
63
62
|
const value = params.get(key);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deep-link.cjs","names":["Linking"
|
|
1
|
+
{"version":3,"file":"deep-link.cjs","names":["Linking"],"sources":["../../src/native/deep-link.ts"],"sourcesContent":["import { AppState, Linking } from 'react-native';\nimport type { DeepLink } from '../setup/index';\nimport type { TrackTags, UTMParams } from '../track/types';\n\n/**\n * The link that opened the app for this visit, the app's landing page: `getTags` merges its URL\n * (as `page_location`) and utm into every event's tags, so the session it starts is\n * classified as a web page's URL would be. A universal link or an app link from an email or an ad\n * opens the app rather than the site, and without this every one of those sessions was direct.\n *\n * The visit lasts from the open to the app going to the background, so every event in between\n * carries the link, as every event of a web page carries that page's query; the next open, with a\n * link or without, starts from nothing. Only the event that starts a session gets it classified:\n * a link tapped while the session is still running joins it, as GA4 keeps one session across a\n * change of campaign.\n *\n * Read from React Native's `Linking`: the URL that launched the process, and each one that brings\n * the running app to the front. Pass it to `setupAnalytics({ deepLink })`, which starts the\n * listening; nothing happens at import.\n */\nlet openUrl: string | undefined;\n\n/**\n * The parameters a deep link carries: the utm its sender tagged it with. An ad platform's click id\n * rides on the web landing page, not on a link into the app.\n */\nconst UTM_KEYS = [\n 'utm_source',\n 'utm_medium',\n 'utm_campaign',\n 'utm_term',\n 'utm_content',\n 'utm_id',\n 'utm_source_platform',\n 'utm_creative_format',\n 'utm_marketing_tactic',\n] as const satisfies readonly (keyof UTMParams)[];\nlet initialUrlPromise: Promise<void> | undefined;\nlet listening = false;\n\n/** The URL that launched the process, read once; a link that arrived since has the last word. */\nfunction resolveInitialUrl(): Promise<void> {\n initialUrlPromise ??= Linking.getInitialURL().then(\n (url) => {\n if (url && openUrl === undefined) openUrl = url;\n },\n () => undefined\n );\n return initialUrlPromise;\n}\n\nexport const deepLink: DeepLink & {\n /** The link this visit opened with, for an entry `Linking` does not see, such as a push notification. */\n open: (url: string | null | undefined) => void;\n} = {\n listen() {\n if (listening) return;\n listening = true;\n Linking.addEventListener('url', ({ url }) => deepLink.open(url));\n // `background`, not `inactive`: iOS goes inactive for a system sheet or the control center,\n // with the visit still on.\n AppState.addEventListener('change', (state) => {\n if (state === 'background') openUrl = undefined;\n });\n },\n /** Waits for the launch URL, so the first event of a launch has it. */\n async getTags(): Promise<TrackTags> {\n await resolveInitialUrl();\n if (!openUrl) return {};\n const at = openUrl.indexOf('?');\n const params = new URLSearchParams(at === -1 ? '' : openUrl.slice(at + 1).split('#')[0]);\n const tags: TrackTags = { page_location: openUrl };\n for (const key of UTM_KEYS) {\n const value = params.get(key);\n if (value !== null) tags[key] = value;\n }\n return tags;\n },\n open(url) {\n openUrl = url || undefined;\n },\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAoBA,IAAI;;;;;AAMJ,MAAM,WAAW;CACf;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;AACA,IAAI;AACJ,IAAI,YAAY;;AAGhB,SAAS,oBAAmC;CAC1C,sBAAsBA,aAAAA,QAAQ,cAAc,CAAC,CAAC,MAC3C,QAAQ;EACP,IAAI,OAAO,YAAY,KAAA,GAAW,UAAU;CAC9C,SACM,KAAA,CACR;CACA,OAAO;AACT;AAEA,MAAa,WAGT;CACF,SAAS;EACP,IAAI,WAAW;EACf,YAAY;EACZ,aAAA,QAAQ,iBAAiB,QAAQ,EAAE,UAAU,SAAS,KAAK,GAAG,CAAC;EAG/D,aAAA,SAAS,iBAAiB,WAAW,UAAU;GAC7C,IAAI,UAAU,cAAc,UAAU,KAAA;EACxC,CAAC;CACH;;CAEA,MAAM,UAA8B;EAClC,MAAM,kBAAkB;EACxB,IAAI,CAAC,SAAS,OAAO,CAAC;EACtB,MAAM,KAAK,QAAQ,QAAQ,GAAG;EAC9B,MAAM,SAAS,IAAI,gBAAgB,OAAO,KAAK,KAAK,QAAQ,MAAM,KAAK,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,EAAE;EACvF,MAAM,OAAkB,EAAE,eAAe,QAAQ;EACjD,KAAK,MAAM,OAAO,UAAU;GAC1B,MAAM,QAAQ,OAAO,IAAI,GAAG;GAC5B,IAAI,UAAU,MAAM,KAAK,OAAO;EAClC;EACA,OAAO;CACT;CACA,KAAK,KAAK;EACR,UAAU,OAAO,KAAA;CACnB;AACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deep-link.d.cts","names":[],"sources":["../../src/native/deep-link.ts"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"deep-link.d.cts","names":[],"sources":["../../src/native/deep-link.ts"],"mappings":";;qBAmDa,UAAU;;EAErB,OAAO"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deep-link.d.mts","names":[],"sources":["../../src/native/deep-link.ts"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"deep-link.d.mts","names":[],"sources":["../../src/native/deep-link.ts"],"mappings":";;qBAmDa,UAAU;;EAErB,OAAO"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deep-link.mjs","names":[],"sources":["../../src/native/deep-link.ts"],"sourcesContent":["import { AppState, Linking } from 'react-native';\nimport
|
|
1
|
+
{"version":3,"file":"deep-link.mjs","names":[],"sources":["../../src/native/deep-link.ts"],"sourcesContent":["import { AppState, Linking } from 'react-native';\nimport type { DeepLink } from '../setup/index';\nimport type { TrackTags, UTMParams } from '../track/types';\n\n/**\n * The link that opened the app for this visit, the app's landing page: `getTags` merges its URL\n * (as `page_location`) and utm into every event's tags, so the session it starts is\n * classified as a web page's URL would be. A universal link or an app link from an email or an ad\n * opens the app rather than the site, and without this every one of those sessions was direct.\n *\n * The visit lasts from the open to the app going to the background, so every event in between\n * carries the link, as every event of a web page carries that page's query; the next open, with a\n * link or without, starts from nothing. Only the event that starts a session gets it classified:\n * a link tapped while the session is still running joins it, as GA4 keeps one session across a\n * change of campaign.\n *\n * Read from React Native's `Linking`: the URL that launched the process, and each one that brings\n * the running app to the front. Pass it to `setupAnalytics({ deepLink })`, which starts the\n * listening; nothing happens at import.\n */\nlet openUrl: string | undefined;\n\n/**\n * The parameters a deep link carries: the utm its sender tagged it with. An ad platform's click id\n * rides on the web landing page, not on a link into the app.\n */\nconst UTM_KEYS = [\n 'utm_source',\n 'utm_medium',\n 'utm_campaign',\n 'utm_term',\n 'utm_content',\n 'utm_id',\n 'utm_source_platform',\n 'utm_creative_format',\n 'utm_marketing_tactic',\n] as const satisfies readonly (keyof UTMParams)[];\nlet initialUrlPromise: Promise<void> | undefined;\nlet listening = false;\n\n/** The URL that launched the process, read once; a link that arrived since has the last word. */\nfunction resolveInitialUrl(): Promise<void> {\n initialUrlPromise ??= Linking.getInitialURL().then(\n (url) => {\n if (url && openUrl === undefined) openUrl = url;\n },\n () => undefined\n );\n return initialUrlPromise;\n}\n\nexport const deepLink: DeepLink & {\n /** The link this visit opened with, for an entry `Linking` does not see, such as a push notification. */\n open: (url: string | null | undefined) => void;\n} = {\n listen() {\n if (listening) return;\n listening = true;\n Linking.addEventListener('url', ({ url }) => deepLink.open(url));\n // `background`, not `inactive`: iOS goes inactive for a system sheet or the control center,\n // with the visit still on.\n AppState.addEventListener('change', (state) => {\n if (state === 'background') openUrl = undefined;\n });\n },\n /** Waits for the launch URL, so the first event of a launch has it. */\n async getTags(): Promise<TrackTags> {\n await resolveInitialUrl();\n if (!openUrl) return {};\n const at = openUrl.indexOf('?');\n const params = new URLSearchParams(at === -1 ? '' : openUrl.slice(at + 1).split('#')[0]);\n const tags: TrackTags = { page_location: openUrl };\n for (const key of UTM_KEYS) {\n const value = params.get(key);\n if (value !== null) tags[key] = value;\n }\n return tags;\n },\n open(url) {\n openUrl = url || undefined;\n },\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAoBA,IAAI;;;;;AAMJ,MAAM,WAAW;CACf;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;AACA,IAAI;AACJ,IAAI,YAAY;;AAGhB,SAAS,oBAAmC;CAC1C,sBAAsB,QAAQ,cAAc,CAAC,CAAC,MAC3C,QAAQ;EACP,IAAI,OAAO,YAAY,KAAA,GAAW,UAAU;CAC9C,SACM,KAAA,CACR;CACA,OAAO;AACT;AAEA,MAAa,WAGT;CACF,SAAS;EACP,IAAI,WAAW;EACf,YAAY;EACZ,QAAQ,iBAAiB,QAAQ,EAAE,UAAU,SAAS,KAAK,GAAG,CAAC;EAG/D,SAAS,iBAAiB,WAAW,UAAU;GAC7C,IAAI,UAAU,cAAc,UAAU,KAAA;EACxC,CAAC;CACH;;CAEA,MAAM,UAA8B;EAClC,MAAM,kBAAkB;EACxB,IAAI,CAAC,SAAS,OAAO,CAAC;EACtB,MAAM,KAAK,QAAQ,QAAQ,GAAG;EAC9B,MAAM,SAAS,IAAI,gBAAgB,OAAO,KAAK,KAAK,QAAQ,MAAM,KAAK,CAAC,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,EAAE;EACvF,MAAM,OAAkB,EAAE,eAAe,QAAQ;EACjD,KAAK,MAAM,OAAO,UAAU;GAC1B,MAAM,QAAQ,OAAO,IAAI,GAAG;GAC5B,IAAI,UAAU,MAAM,KAAK,OAAO;EAClC;EACA,OAAO;CACT;CACA,KAAK,KAAK;EACR,UAAU,OAAO,KAAA;CACnB;AACF"}
|
package/dist/native/setup.cjs
CHANGED
|
@@ -2,7 +2,6 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
2
2
|
const require_setup_index = require("../setup/index.cjs");
|
|
3
3
|
const require_constants_storage = require("../constants/storage.cjs");
|
|
4
4
|
let react_native = require("react-native");
|
|
5
|
-
let react_native_url_polyfill = require("react-native-url-polyfill");
|
|
6
5
|
let expo_application = require("expo-application");
|
|
7
6
|
let expo_device = require("expo-device");
|
|
8
7
|
let expo_localization = require("expo-localization");
|
|
@@ -104,7 +103,7 @@ async function getTags() {
|
|
|
104
103
|
const screen_height = Math.floor(screen.height);
|
|
105
104
|
const install_referrer = await getInstallReferrer();
|
|
106
105
|
if (install_referrer && installLaunch === void 0) installLaunch = claimInstallReferrer();
|
|
107
|
-
const params = new
|
|
106
|
+
const params = new URLSearchParams(installLaunch ? install_referrer : void 0);
|
|
108
107
|
const opening = await require_setup_index.config.deepLink?.getTags();
|
|
109
108
|
const tags = {
|
|
110
109
|
os: `${expo_device.osName} ${expo_device.osVersion}`,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setup.cjs","names":["Platform","getInstallReferrerAsync","getIosIdForVendorAsync","getAndroidId","randomUUID","deviceType","DeviceType","config","keys","Dimensions","
|
|
1
|
+
{"version":3,"file":"setup.cjs","names":["Platform","getInstallReferrerAsync","getIosIdForVendorAsync","getAndroidId","randomUUID","deviceType","DeviceType","config","keys","Dimensions","osName","osVersion","modelName","manufacturer","modelId","PixelRatio","getLocales","getCalendars","getAdvertisingId"],"sources":["../../src/native/setup.ts"],"sourcesContent":["import 'expo-sqlite/localStorage/install';\nimport { getAndroidId, getInstallReferrerAsync, getIosIdForVendorAsync } from 'expo-application';\nimport { randomUUID } from 'expo-crypto';\nimport {\n DeviceType,\n deviceType,\n manufacturer,\n modelId,\n modelName,\n osName,\n osVersion,\n} from 'expo-device';\nimport { getCalendars, getLocales } from 'expo-localization';\nimport { getAdvertisingId } from 'expo-tracking-transparency';\nimport { Dimensions, PixelRatio, Platform } from 'react-native';\nimport { keys } from '../constants/storage';\nimport { type Storage, cache, config } from '../setup/index';\nimport type { TrackTags } from '../track/types';\n\nconst map = new Map<string, string>();\n\nexport const storage: Storage = {\n getItem: (key) => {\n try {\n return localStorage.getItem(key);\n } catch {\n console.error('localStorage is not available');\n return map.get(key) ?? null;\n }\n },\n setItem: (key, value) => {\n try {\n localStorage.setItem(key, value);\n } catch {\n console.error('localStorage is not available');\n map.set(key, value);\n }\n },\n};\n\n/**\n * `getTags` runs once per event now, and both of these reach for a native module. Neither answer\n * can change while the app is running, so each is resolved once and the promise is reused. A\n * failed lookup is not cached, so the next event tries again.\n */\nlet deviceIdPromise: Promise<string> | undefined;\nlet installReferrerPromise: Promise<string | undefined> | undefined;\n\nexport function getDeviceId(): Promise<string> {\n deviceIdPromise ??= resolveDeviceId().catch((error: unknown) => {\n deviceIdPromise = undefined;\n throw error;\n });\n return deviceIdPromise;\n}\n\nfunction getInstallReferrer(): Promise<string | undefined> {\n if (Platform.OS !== 'android') return Promise.resolve(undefined);\n installReferrerPromise ??= getInstallReferrerAsync().catch((error: unknown) => {\n installReferrerPromise = undefined;\n throw error;\n });\n return installReferrerPromise;\n}\n\nasync function resolveDeviceId(): Promise<string> {\n let deviceId: string | null = null;\n if (Platform.OS === 'ios') {\n deviceId = await getIosIdForVendorAsync();\n } else if (Platform.OS === 'android') {\n deviceId = getAndroidId();\n }\n if (!deviceId) {\n deviceId = localStorage.getItem('device_id');\n if (!deviceId) {\n deviceId = randomUUID();\n localStorage.setItem('device_id', deviceId);\n }\n }\n return deviceId;\n}\n\nexport function getDeviceType(): string | undefined {\n switch (deviceType) {\n case DeviceType.PHONE:\n return 'mobile';\n case DeviceType.TABLET:\n return 'tablet';\n case DeviceType.DESKTOP:\n return 'desktop';\n case DeviceType.TV:\n return 'smarttv';\n default:\n return undefined;\n }\n}\n\n/**\n * The install referrer's utm belongs to one launch: the first one that has the referrer in hand\n * — normally the install launch, since `first_open` is the first event the app tracks and\n * tracking it builds the first tags. That launch claims the referrer by writing a marker of its\n * own, and holds the claim for the rest of the process, so every event of the launch and the\n * visitor created from it carry the utm; every later launch finds the marker and sends the raw\n * `install_referrer` alone. Cleared storage and a reinstall lose the marker and claim again,\n * exactly as they make `first_open` fire again.\n *\n * Claimed only once a referrer has actually been resolved. The Install Referrer API needs the\n * Play Store service, which is at its least available right after an install; a launch that\n * could not reach it must not spend the claim on nothing, because the referrer stays readable\n * for 90 days and a later launch can still take it — late attribution, rather than none. iOS\n * has no referrer and so never claims.\n *\n * A marker of its own rather than `first_open_time`, so that nothing here depends on when the\n * hook writes that one relative to the first `getTags`. The storage read waits for that call:\n * `setupAnalytics` and this module's evaluation can run where no storage exists.\n */\nlet installLaunch: boolean | undefined;\n\nfunction claimInstallReferrer(): boolean {\n if (config.storage.getItem(keys.install_referrer_claim_time)) return false;\n config.storage.setItem(keys.install_referrer_claim_time, new Date().toISOString());\n return true;\n}\n\nexport async function getTags(): Promise<TrackTags> {\n const screen = Dimensions.get('screen');\n const screen_width = Math.floor(screen.width);\n const screen_height = Math.floor(screen.height);\n\n // The install referrer never changes, and it used to be spread into `utm_*` on every launch —\n // so every session of an Android install reported the install campaign as its own acquisition\n // for as long as the app stayed installed, and a report reading a session's tags could not tell\n // the install from the thousandth open. The campaign is the install's touch, not every\n // session's.\n const install_referrer = await getInstallReferrer();\n if (install_referrer && installLaunch === undefined) installLaunch = claimInstallReferrer();\n const params = new URLSearchParams(installLaunch ? install_referrer : undefined);\n const opening = await config.deepLink?.getTags();\n\n const tags: TrackTags = {\n os: `${osName} ${osVersion}`,\n os_name: osName ?? undefined,\n os_version: osVersion ?? undefined,\n device: modelName ?? undefined,\n device_id: await getDeviceId(),\n device_type: getDeviceType(),\n device_vendor: manufacturer ?? undefined,\n device_model_id: modelId ?? undefined,\n device_pixel_ratio: PixelRatio.get(),\n screen_width,\n screen_height,\n screen_resolution: `${screen_width}x${screen_height}`,\n release: config.release,\n language: getLocales().at(0)?.languageTag ?? 'en',\n time_zone: getCalendars().at(0)?.timeZone ?? 'UTC',\n // ads\n advertising_id: getAdvertisingId() ?? undefined,\n install_referrer,\n // utm params: from the install referrer, on the launch that claimed it\n utm_source: params.get('utm_source') ?? undefined,\n utm_medium: params.get('utm_medium') ?? undefined,\n utm_campaign: params.get('utm_campaign') ?? undefined,\n utm_term: params.get('utm_term') ?? undefined,\n utm_content: params.get('utm_content') ?? undefined,\n utm_id: params.get('utm_id') ?? undefined,\n utm_source_platform: params.get('utm_source_platform') ?? undefined,\n utm_creative_format: params.get('utm_creative_format') ?? undefined,\n utm_marketing_tactic: params.get('utm_marketing_tactic') ?? undefined,\n // the link that opened the app (`setupAnalytics({ deepLink })`): its URL, utm and click ids,\n // over the install referrer's\n ...opening,\n };\n\n cache.tags = tags;\n return tags;\n}\n"],"mappings":";;;;;;;;;;;AAmBA,MAAM,sBAAM,IAAI,IAAoB;AAEpC,MAAa,UAAmB;CAC9B,UAAU,QAAQ;EAChB,IAAI;GACF,OAAO,aAAa,QAAQ,GAAG;EACjC,QAAQ;GACN,QAAQ,MAAM,+BAA+B;GAC7C,OAAO,IAAI,IAAI,GAAG,KAAK;EACzB;CACF;CACA,UAAU,KAAK,UAAU;EACvB,IAAI;GACF,aAAa,QAAQ,KAAK,KAAK;EACjC,QAAQ;GACN,QAAQ,MAAM,+BAA+B;GAC7C,IAAI,IAAI,KAAK,KAAK;EACpB;CACF;AACF;;;;;;AAOA,IAAI;AACJ,IAAI;AAEJ,SAAgB,cAA+B;CAC7C,oBAAoB,gBAAgB,CAAC,CAAC,OAAO,UAAmB;EAC9D,kBAAkB,KAAA;EAClB,MAAM;CACR,CAAC;CACD,OAAO;AACT;AAEA,SAAS,qBAAkD;CACzD,IAAIA,aAAAA,SAAS,OAAO,WAAW,OAAO,QAAQ,QAAQ,KAAA,CAAS;CAC/D,4BAAA,GAA2BC,iBAAAA,wBAAAA,CAAwB,CAAC,CAAC,OAAO,UAAmB;EAC7E,yBAAyB,KAAA;EACzB,MAAM;CACR,CAAC;CACD,OAAO;AACT;AAEA,eAAe,kBAAmC;CAChD,IAAI,WAA0B;CAC9B,IAAID,aAAAA,SAAS,OAAO,OAClB,WAAW,OAAA,GAAME,iBAAAA,uBAAAA,CAAuB;MAC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A;EACrB,WAAW,MAAM,YAAY;EAC7B,aAAa,cAAc;EAC3B,eAAeC,YAAAA,gBAAgB,KAAA;EAC/B,iBAAiBC,YAAAA,WAAW,KAAA;EAC5B,oBAAoBC,aAAAA,WAAW,IAAI;EACnC;EACA;EACA,mBAAmB,GAAG,aAAa,GAAG;EACtC,SAASR,oBAAAA,OAAO;EAChB,WAAA,GAAUS,kBAAAA,WAAAA,CAAW,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,eAAe;EAC7C,YAAA,GAAWC,kBAAAA,aAAAA,CAAa,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,YAAY;EAE7C,iBAAA,GAAgBC,2BAAAA,iBAAAA,CAAiB,KAAK,KAAA;EACtC;EAEA,YAAY,OAAO,IAAI,YAAY,KAAK,KAAA;EACxC,YAAY,OAAO,IAAI,YAAY,KAAK,KAAA;EACxC,cAAc,OAAO,IAAI,cAAc,KAAK,KAAA;EAC5C,UAAU,OAAO,IAAI,UAAU,KAAK,KAAA;EACpC,aAAa,OAAO,IAAI,aAAa,KAAK,KAAA;EAC1C,QAAQ,OAAO,IAAI,QAAQ,KAAK,KAAA;EAChC,qBAAqB,OAAO,IAAI,qBAAqB,KAAK,KAAA;EAC1D,qBAAqB,OAAO,IAAI,qBAAqB,KAAK,KAAA;EAC1D,sBAAsB,OAAO,IAAI,sBAAsB,KAAK,KAAA;EAG5D,GAAG;CACL;CAEA,oBAAA,MAAM,OAAO;CACb,OAAO;AACT"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setup.d.cts","names":[],"sources":["../../src/native/setup.ts"],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"setup.d.cts","names":[],"sources":["../../src/native/setup.ts"],"mappings":";;;;qBAqBa,SAAS;wBA2BN,eAAe;wBAkCf;wBA0CM,WAAW,QAAQ"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setup.d.mts","names":[],"sources":["../../src/native/setup.ts"],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"setup.d.mts","names":[],"sources":["../../src/native/setup.ts"],"mappings":";;;;qBAqBa,SAAS;wBA2BN,eAAe;wBAkCf;wBA0CM,WAAW,QAAQ"}
|
package/dist/native/setup.mjs
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { cache, config } from "../setup/index.mjs";
|
|
2
2
|
import { keys } from "../constants/storage.mjs";
|
|
3
3
|
import { Dimensions, PixelRatio, Platform } from "react-native";
|
|
4
|
-
import { URLSearchParams } from "react-native-url-polyfill";
|
|
5
4
|
import { getAndroidId, getInstallReferrerAsync, getIosIdForVendorAsync } from "expo-application";
|
|
6
5
|
import { DeviceType, deviceType, manufacturer, modelId, modelName, osName, osVersion } from "expo-device";
|
|
7
6
|
import { getCalendars, getLocales } from "expo-localization";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setup.mjs","names":[],"sources":["../../src/native/setup.ts"],"sourcesContent":["import 'expo-sqlite/localStorage/install';\nimport { getAndroidId, getInstallReferrerAsync, getIosIdForVendorAsync } from 'expo-application';\nimport { randomUUID } from 'expo-crypto';\nimport {\n DeviceType,\n deviceType,\n manufacturer,\n modelId,\n modelName,\n osName,\n osVersion,\n} from 'expo-device';\nimport { getCalendars, getLocales } from 'expo-localization';\nimport { getAdvertisingId } from 'expo-tracking-transparency';\nimport { Dimensions, PixelRatio, Platform } from 'react-native';\nimport {
|
|
1
|
+
{"version":3,"file":"setup.mjs","names":[],"sources":["../../src/native/setup.ts"],"sourcesContent":["import 'expo-sqlite/localStorage/install';\nimport { getAndroidId, getInstallReferrerAsync, getIosIdForVendorAsync } from 'expo-application';\nimport { randomUUID } from 'expo-crypto';\nimport {\n DeviceType,\n deviceType,\n manufacturer,\n modelId,\n modelName,\n osName,\n osVersion,\n} from 'expo-device';\nimport { getCalendars, getLocales } from 'expo-localization';\nimport { getAdvertisingId } from 'expo-tracking-transparency';\nimport { Dimensions, PixelRatio, Platform } from 'react-native';\nimport { keys } from '../constants/storage';\nimport { type Storage, cache, config } from '../setup/index';\nimport type { TrackTags } from '../track/types';\n\nconst map = new Map<string, string>();\n\nexport const storage: Storage = {\n getItem: (key) => {\n try {\n return localStorage.getItem(key);\n } catch {\n console.error('localStorage is not available');\n return map.get(key) ?? null;\n }\n },\n setItem: (key, value) => {\n try {\n localStorage.setItem(key, value);\n } catch {\n console.error('localStorage is not available');\n map.set(key, value);\n }\n },\n};\n\n/**\n * `getTags` runs once per event now, and both of these reach for a native module. Neither answer\n * can change while the app is running, so each is resolved once and the promise is reused. A\n * failed lookup is not cached, so the next event tries again.\n */\nlet deviceIdPromise: Promise<string> | undefined;\nlet installReferrerPromise: Promise<string | undefined> | undefined;\n\nexport function getDeviceId(): Promise<string> {\n deviceIdPromise ??= resolveDeviceId().catch((error: unknown) => {\n deviceIdPromise = undefined;\n throw error;\n });\n return deviceIdPromise;\n}\n\nfunction getInstallReferrer(): Promise<string | undefined> {\n if (Platform.OS !== 'android') return Promise.resolve(undefined);\n installReferrerPromise ??= getInstallReferrerAsync().catch((error: unknown) => {\n installReferrerPromise = undefined;\n throw error;\n });\n return installReferrerPromise;\n}\n\nasync function resolveDeviceId(): Promise<string> {\n let deviceId: string | null = null;\n if (Platform.OS === 'ios') {\n deviceId = await getIosIdForVendorAsync();\n } else if (Platform.OS === 'android') {\n deviceId = getAndroidId();\n }\n if (!deviceId) {\n deviceId = localStorage.getItem('device_id');\n if (!deviceId) {\n deviceId = randomUUID();\n localStorage.setItem('device_id', deviceId);\n }\n }\n return deviceId;\n}\n\nexport function getDeviceType(): string | undefined {\n switch (deviceType) {\n case DeviceType.PHONE:\n return 'mobile';\n case DeviceType.TABLET:\n return 'tablet';\n case DeviceType.DESKTOP:\n return 'desktop';\n case DeviceType.TV:\n return 'smarttv';\n default:\n return undefined;\n }\n}\n\n/**\n * The install referrer's utm belongs to one launch: the first one that has the referrer in hand\n * — normally the install launch, since `first_open` is the first event the app tracks and\n * tracking it builds the first tags. That launch claims the referrer by writing a marker of its\n * own, and holds the claim for the rest of the process, so every event of the launch and the\n * visitor created from it carry the utm; every later launch finds the marker and sends the raw\n * `install_referrer` alone. Cleared storage and a reinstall lose the marker and claim again,\n * exactly as they make `first_open` fire again.\n *\n * Claimed only once a referrer has actually been resolved. The Install Referrer API needs the\n * Play Store service, which is at its least available right after an install; a launch that\n * could not reach it must not spend the claim on nothing, because the referrer stays readable\n * for 90 days and a later launch can still take it — late attribution, rather than none. iOS\n * has no referrer and so never claims.\n *\n * A marker of its own rather than `first_open_time`, so that nothing here depends on when the\n * hook writes that one relative to the first `getTags`. The storage read waits for that call:\n * `setupAnalytics` and this module's evaluation can run where no storage exists.\n */\nlet installLaunch: boolean | undefined;\n\nfunction claimInstallReferrer(): boolean {\n if (config.storage.getItem(keys.install_referrer_claim_time)) return false;\n config.storage.setItem(keys.install_referrer_claim_time, new Date().toISOString());\n return true;\n}\n\nexport async function getTags(): Promise<TrackTags> {\n const screen = Dimensions.get('screen');\n const screen_width = Math.floor(screen.width);\n const screen_height = Math.floor(screen.height);\n\n // The install referrer never changes, and it used to be spread into `utm_*` on every launch —\n // so every session of an Android install reported the install campaign as its own acquisition\n // for as long as the app stayed installed, and a report reading a session's tags could not tell\n // the install from the thousandth open. The campaign is the install's touch, not every\n // session's.\n const install_referrer = await getInstallReferrer();\n if (install_referrer && installLaunch === undefined) installLaunch = claimInstallReferrer();\n const params = new URLSearchParams(installLaunch ? install_referrer : undefined);\n const opening = await config.deepLink?.getTags();\n\n const tags: TrackTags = {\n os: `${osName} ${osVersion}`,\n os_name: osName ?? undefined,\n os_version: osVersion ?? undefined,\n device: modelName ?? undefined,\n device_id: await getDeviceId(),\n device_type: getDeviceType(),\n device_vendor: manufacturer ?? undefined,\n device_model_id: modelId ?? undefined,\n device_pixel_ratio: PixelRatio.get(),\n screen_width,\n screen_height,\n screen_resolution: `${screen_width}x${screen_height}`,\n release: config.release,\n language: getLocales().at(0)?.languageTag ?? 'en',\n time_zone: getCalendars().at(0)?.timeZone ?? 'UTC',\n // ads\n advertising_id: getAdvertisingId() ?? undefined,\n install_referrer,\n // utm params: from the install referrer, on the launch that claimed it\n utm_source: params.get('utm_source') ?? undefined,\n utm_medium: params.get('utm_medium') ?? undefined,\n utm_campaign: params.get('utm_campaign') ?? undefined,\n utm_term: params.get('utm_term') ?? undefined,\n utm_content: params.get('utm_content') ?? undefined,\n utm_id: params.get('utm_id') ?? undefined,\n utm_source_platform: params.get('utm_source_platform') ?? undefined,\n utm_creative_format: params.get('utm_creative_format') ?? undefined,\n utm_marketing_tactic: params.get('utm_marketing_tactic') ?? undefined,\n // the link that opened the app (`setupAnalytics({ deepLink })`): its URL, utm and click ids,\n // over the install referrer's\n ...opening,\n };\n\n cache.tags = tags;\n return tags;\n}\n"],"mappings":";;;;;;;;;;AAmBA,MAAM,sBAAM,IAAI,IAAoB;AAEpC,MAAa,UAAmB;CAC9B,UAAU,QAAQ;EAChB,IAAI;GACF,OAAO,aAAa,QAAQ,GAAG;EACjC,QAAQ;GACN,QAAQ,MAAM,+BAA+B;GAC7C,OAAO,IAAI,IAAI,GAAG,KAAK;EACzB;CACF;CACA,UAAU,KAAK,UAAU;EACvB,IAAI;GACF,aAAa,QAAQ,KAAK,KAAK;EACjC,QAAQ;GACN,QAAQ,MAAM,+BAA+B;GAC7C,IAAI,IAAI,KAAK,KAAK;EACpB;CACF;AACF;;;;;;AAOA,IAAI;AACJ,IAAI;AAEJ,SAAgB,cAA+B;CAC7C,oBAAoB,gBAAgB,CAAC,CAAC,OAAO,UAAmB;EAC9D,kBAAkB,KAAA;EAClB,MAAM;CACR,CAAC;CACD,OAAO;AACT;AAEA,SAAS,qBAAkD;CACzD,IAAI,SAAS,OAAO,WAAW,OAAO,QAAQ,QAAQ,KAAA,CAAS;CAC/D,2BAA2B,wBAAwB,CAAC,CAAC,OAAO,UAAmB;EAC7E,yBAAyB,KAAA;EACzB,MAAM;CACR,CAAC;CACD,OAAO;AACT;AAEA,eAAe,kBAAmC;CAChD,I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AAW,IAAI;EACnC;EACA;EACA,mBAAmB,GAAG,aAAa,GAAG;EACtC,SAAS,OAAO;EAChB,UAAU,WAAW,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,eAAe;EAC7C,WAAW,aAAa,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,YAAY;EAE7C,gBAAgB,iBAAiB,KAAK,KAAA;EACtC;EAEA,YAAY,OAAO,IAAI,YAAY,KAAK,KAAA;EACxC,YAAY,OAAO,IAAI,YAAY,KAAK,KAAA;EACxC,cAAc,OAAO,IAAI,cAAc,KAAK,KAAA;EAC5C,UAAU,OAAO,IAAI,UAAU,KAAK,KAAA;EACpC,aAAa,OAAO,IAAI,aAAa,KAAK,KAAA;EAC1C,QAAQ,OAAO,IAAI,QAAQ,KAAK,KAAA;EAChC,qBAAqB,OAAO,IAAI,qBAAqB,KAAK,KAAA;EAC1D,qBAAqB,OAAO,IAAI,qBAAqB,KAAK,KAAA;EAC1D,sBAAsB,OAAO,IAAI,sBAAsB,KAAK,KAAA;EAG5D,GAAG;CACL;CAEA,MAAM,OAAO;CACb,OAAO;AACT"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shware/analytics",
|
|
3
|
-
"version": "11.13.
|
|
3
|
+
"version": "11.13.2",
|
|
4
4
|
"private": false,
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -156,19 +156,22 @@
|
|
|
156
156
|
"access": "public"
|
|
157
157
|
},
|
|
158
158
|
"dependencies": {
|
|
159
|
-
"@shware/utils": "^1.
|
|
159
|
+
"@shware/utils": "^1.7.0",
|
|
160
160
|
"capi-param-builder-nodejs": "~1.3.2",
|
|
161
161
|
"cookie": "^2.0.1",
|
|
162
162
|
"uuid": "^14.0.3",
|
|
163
163
|
"web-vitals": "^6.2.3"
|
|
164
164
|
},
|
|
165
165
|
"devDependencies": {
|
|
166
|
+
"@babel/core": "7.29.7",
|
|
166
167
|
"@repo/typescript-config": "0.0.0",
|
|
167
168
|
"@testing-library/react": "^16.3.3",
|
|
169
|
+
"@types/babel__core": "7.20.5",
|
|
168
170
|
"@types/facebook-nodejs-business-sdk": "^24.0.0",
|
|
169
171
|
"@types/node": "^26.6.5",
|
|
170
172
|
"@types/react": "^19.3.0",
|
|
171
173
|
"@vitest/coverage-v8": "^5.0.5",
|
|
174
|
+
"babel-plugin-react-compiler": "1.0.0",
|
|
172
175
|
"bowser": "^2.14.1",
|
|
173
176
|
"jsdom": "^30.1.3",
|
|
174
177
|
"react-dom": "^19.3.1",
|
|
@@ -193,7 +196,6 @@
|
|
|
193
196
|
"react": "^19.3.0",
|
|
194
197
|
"react-native": "^0.87.1 || ^0.88.0",
|
|
195
198
|
"react-native-fbsdk-next": "^13.4.3",
|
|
196
|
-
"react-native-url-polyfill": "^4.0.0",
|
|
197
199
|
"react-router": "^8.4.0",
|
|
198
200
|
"zod": "^4.6.5"
|
|
199
201
|
},
|
|
@@ -246,9 +248,6 @@
|
|
|
246
248
|
"react-native-fbsdk-next": {
|
|
247
249
|
"optional": true
|
|
248
250
|
},
|
|
249
|
-
"react-native-url-polyfill": {
|
|
250
|
-
"optional": true
|
|
251
|
-
},
|
|
252
251
|
"react-router": {
|
|
253
252
|
"optional": true
|
|
254
253
|
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
let react = require("react");
|
|
3
|
-
//#region src/hooks/use-previous.ts
|
|
4
|
-
function usePrevious(value) {
|
|
5
|
-
const ref = (0, react.useRef)(void 0);
|
|
6
|
-
(0, react.useEffect)(() => {
|
|
7
|
-
ref.current = value;
|
|
8
|
-
});
|
|
9
|
-
return ref.current;
|
|
10
|
-
}
|
|
11
|
-
//#endregion
|
|
12
|
-
exports.usePrevious = usePrevious;
|
|
13
|
-
|
|
14
|
-
//# sourceMappingURL=use-previous.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"use-previous.cjs","names":["useRef"],"sources":["../../src/hooks/use-previous.ts"],"sourcesContent":["import { useEffect, useRef } from 'react';\n\nexport function usePrevious<T>(value: T): T | undefined {\n const ref = useRef<T | undefined>(undefined);\n useEffect(() => {\n ref.current = value;\n });\n return ref.current;\n}\n"],"mappings":";;;AAEA,SAAgB,YAAe,OAAyB;CACtD,MAAM,OAAA,GAAMA,MAAAA,OAAAA,CAAsB,KAAA,CAAS;CAC3C,CAAA,GAAA,MAAA,UAAA,OAAgB;EACd,IAAI,UAAU;CAChB,CAAC;CACD,OAAO,IAAI;AACb"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"use-previous.d.cts","names":[],"sources":["../../src/hooks/use-previous.ts"],"mappings":";wBAEgB,YAAY,GAAG,OAAO,IAAI"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"use-previous.d.mts","names":[],"sources":["../../src/hooks/use-previous.ts"],"mappings":";wBAEgB,YAAY,GAAG,OAAO,IAAI"}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { useEffect, useRef } from "react";
|
|
2
|
-
//#region src/hooks/use-previous.ts
|
|
3
|
-
function usePrevious(value) {
|
|
4
|
-
const ref = useRef(void 0);
|
|
5
|
-
useEffect(() => {
|
|
6
|
-
ref.current = value;
|
|
7
|
-
});
|
|
8
|
-
return ref.current;
|
|
9
|
-
}
|
|
10
|
-
//#endregion
|
|
11
|
-
export { usePrevious };
|
|
12
|
-
|
|
13
|
-
//# sourceMappingURL=use-previous.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"use-previous.mjs","names":[],"sources":["../../src/hooks/use-previous.ts"],"sourcesContent":["import { useEffect, useRef } from 'react';\n\nexport function usePrevious<T>(value: T): T | undefined {\n const ref = useRef<T | undefined>(undefined);\n useEffect(() => {\n ref.current = value;\n });\n return ref.current;\n}\n"],"mappings":";;AAEA,SAAgB,YAAe,OAAyB;CACtD,MAAM,MAAM,OAAsB,KAAA,CAAS;CAC3C,gBAAgB;EACd,IAAI,UAAU;CAChB,CAAC;CACD,OAAO,IAAI;AACb"}
|