@reforma/site 0.0.3 → 0.0.4
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/dist/analytics.d.ts +7 -3
- package/dist/analytics.d.ts.map +1 -1
- package/dist/analytics.js +28 -14
- package/package.json +1 -1
package/dist/analytics.d.ts
CHANGED
|
@@ -4,9 +4,10 @@ type AnalyticsOptions = {
|
|
|
4
4
|
/** Default false — skip localhost / 127.0.0.1. */
|
|
5
5
|
captureOnLocalhost?: boolean;
|
|
6
6
|
};
|
|
7
|
-
type
|
|
7
|
+
type TrackOptions = {
|
|
8
8
|
url?: string;
|
|
9
9
|
referrer?: string;
|
|
10
|
+
props?: Record<string, string>;
|
|
10
11
|
};
|
|
11
12
|
/**
|
|
12
13
|
* Start first-party pageview collection for a published Reforma site.
|
|
@@ -14,7 +15,10 @@ type TrackProps = {
|
|
|
14
15
|
* multiple times — only the first call installs history hooks.
|
|
15
16
|
*/
|
|
16
17
|
export declare function initAnalytics(options?: AnalyticsOptions): void;
|
|
17
|
-
/**
|
|
18
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Send a named event. `pageview` is traffic (deduped by path); any other name
|
|
20
|
+
* is a custom event / goal conversion. Optional `props` are string key/values.
|
|
21
|
+
*/
|
|
22
|
+
export declare function track(name: string, options?: TrackOptions): void;
|
|
19
23
|
export {};
|
|
20
24
|
//# sourceMappingURL=analytics.d.ts.map
|
package/dist/analytics.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"analytics.d.ts","sourceRoot":"","sources":["../src/analytics.ts"],"names":[],"mappings":"AAAA,KAAK,gBAAgB,GAAG;IACpB,wDAAwD;IACxD,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,kDAAkD;IAClD,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAChC,CAAC;AAEF,KAAK,
|
|
1
|
+
{"version":3,"file":"analytics.d.ts","sourceRoot":"","sources":["../src/analytics.ts"],"names":[],"mappings":"AAAA,KAAK,gBAAgB,GAAG;IACpB,wDAAwD;IACxD,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,kDAAkD;IAClD,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAChC,CAAC;AAEF,KAAK,YAAY,GAAG;IAChB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAClC,CAAC;AASF;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,OAAO,GAAE,gBAAqB,GAAG,IAAI,CAqClE;AAED;;;GAGG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,YAAiB,GAAG,IAAI,CA6DpE"}
|
package/dist/analytics.js
CHANGED
|
@@ -39,25 +39,39 @@ export function initAnalytics(options = {}) {
|
|
|
39
39
|
track('pageview');
|
|
40
40
|
});
|
|
41
41
|
}
|
|
42
|
-
/**
|
|
43
|
-
|
|
44
|
-
|
|
42
|
+
/**
|
|
43
|
+
* Send a named event. `pageview` is traffic (deduped by path); any other name
|
|
44
|
+
* is a custom event / goal conversion. Optional `props` are string key/values.
|
|
45
|
+
*/
|
|
46
|
+
export function track(name, options = {}) {
|
|
47
|
+
if (!enabled || typeof window === 'undefined') {
|
|
45
48
|
return;
|
|
46
49
|
}
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
if (path === lastPath) {
|
|
50
|
+
const eventName = name.trim();
|
|
51
|
+
if (!eventName) {
|
|
50
52
|
return;
|
|
51
53
|
}
|
|
52
|
-
|
|
53
|
-
const
|
|
54
|
+
const url = options.url ?? window.location.href;
|
|
55
|
+
const path = pathOf(url);
|
|
56
|
+
if (eventName === 'pageview') {
|
|
57
|
+
if (path === lastPath) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
lastPath = path;
|
|
61
|
+
}
|
|
62
|
+
const payload = {
|
|
54
63
|
key: writeKey,
|
|
64
|
+
name: eventName,
|
|
55
65
|
url,
|
|
56
|
-
referrer:
|
|
57
|
-
}
|
|
66
|
+
referrer: options.referrer ?? (document.referrer || undefined),
|
|
67
|
+
};
|
|
68
|
+
if (eventName !== 'pageview' && options.props && typeof options.props === 'object') {
|
|
69
|
+
payload.props = options.props;
|
|
70
|
+
}
|
|
71
|
+
const bodyText = JSON.stringify(payload);
|
|
58
72
|
const body = typeof Blob === 'undefined'
|
|
59
|
-
?
|
|
60
|
-
: new Blob([
|
|
73
|
+
? bodyText
|
|
74
|
+
: new Blob([bodyText], { type: 'application/json' });
|
|
61
75
|
try {
|
|
62
76
|
if (typeof navigator !== 'undefined' && typeof navigator.sendBeacon === 'function') {
|
|
63
77
|
const ok = navigator.sendBeacon(endpoint, body);
|
|
@@ -67,7 +81,7 @@ export function track(name, props = {}) {
|
|
|
67
81
|
}
|
|
68
82
|
void fetch(endpoint, {
|
|
69
83
|
method: 'POST',
|
|
70
|
-
body:
|
|
84
|
+
body: bodyText,
|
|
71
85
|
headers: { 'Content-Type': 'application/json' },
|
|
72
86
|
keepalive: true,
|
|
73
87
|
mode: 'cors',
|
|
@@ -113,6 +127,6 @@ function pathOf(url) {
|
|
|
113
127
|
return new URL(url, window.location.origin).pathname;
|
|
114
128
|
}
|
|
115
129
|
catch {
|
|
116
|
-
return
|
|
130
|
+
return '/';
|
|
117
131
|
}
|
|
118
132
|
}
|