aesirx-analytics 1.2.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +1 -3
- package/dist/Consent-7KHERFGJ.js +8106 -0
- package/dist/analytics.js +9479 -40
- package/dist/chunk-JQ6CXYV4.js +34 -0
- package/dist/index.d.ts +5 -7
- package/dist/index.js +276 -1
- package/package.json +5 -3
- package/dist/Consent-QFGYRZ4O.js +0 -1
- package/dist/chunk-D75AKWUD.js +0 -5
@@ -0,0 +1,34 @@
|
|
1
|
+
// src/utils/AnalyticsContextProvider.tsx
|
2
|
+
import React, { useState } from "react";
|
3
|
+
var AnalyticsContext = React.createContext({
|
4
|
+
event_uuid: void 0,
|
5
|
+
visitor_uuid: void 0,
|
6
|
+
setEventID: void 0,
|
7
|
+
setUUID: void 0
|
8
|
+
});
|
9
|
+
var AnalyticsContextProvider = ({ children }) => {
|
10
|
+
const [eventID, setEventID] = useState();
|
11
|
+
const [UUID, setUUID] = useState();
|
12
|
+
return /* @__PURE__ */ React.createElement(
|
13
|
+
AnalyticsContext.Provider,
|
14
|
+
{
|
15
|
+
value: {
|
16
|
+
event_uuid: eventID,
|
17
|
+
visitor_uuid: UUID,
|
18
|
+
setEventID,
|
19
|
+
setUUID
|
20
|
+
}
|
21
|
+
},
|
22
|
+
children
|
23
|
+
);
|
24
|
+
};
|
25
|
+
var AnalyticsContextProvider_default = AnalyticsContextProvider;
|
26
|
+
|
27
|
+
export {
|
28
|
+
AnalyticsContext,
|
29
|
+
AnalyticsContextProvider_default
|
30
|
+
};
|
31
|
+
/*
|
32
|
+
* @copyright Copyright (C) 2022 AesirX. All rights reserved.
|
33
|
+
* @license GNU General Public License version 3, see LICENSE.
|
34
|
+
*/
|
package/dist/index.d.ts
CHANGED
@@ -5,7 +5,7 @@ interface AnalyticsNext {
|
|
5
5
|
router: NextRouter;
|
6
6
|
children?: ReactNode;
|
7
7
|
}
|
8
|
-
declare const AnalyticsNext: ({ router, children }: AnalyticsNext) => JSX.Element;
|
8
|
+
declare const AnalyticsNext: ({ router, children }: AnalyticsNext) => React.JSX.Element;
|
9
9
|
|
10
10
|
interface AnalyticsReact {
|
11
11
|
location: {
|
@@ -17,17 +17,15 @@ interface AnalyticsReact {
|
|
17
17
|
};
|
18
18
|
children?: ReactNode;
|
19
19
|
}
|
20
|
-
declare const AnalyticsReact: ({ location, history, children }: AnalyticsReact) => JSX.Element;
|
20
|
+
declare const AnalyticsReact: ({ location, history, children }: AnalyticsReact) => React.JSX.Element;
|
21
21
|
|
22
|
-
declare const trackEvent: (endpoint: string,
|
22
|
+
declare const trackEvent: (endpoint: string, referer?: string, data?: object) => Promise<boolean>;
|
23
23
|
|
24
24
|
interface AnalyticsContextType {
|
25
|
+
event_uuid: string;
|
25
26
|
visitor_uuid: string;
|
26
|
-
|
27
|
-
visitor_uuid_start: string;
|
27
|
+
setEventID: Dispatch<SetStateAction<string>>;
|
28
28
|
setUUID: Dispatch<SetStateAction<string>>;
|
29
|
-
setEventIDStart: Dispatch<SetStateAction<string>>;
|
30
|
-
setUUIDStart: Dispatch<SetStateAction<string>>;
|
31
29
|
}
|
32
30
|
declare const AnalyticsContext: React.Context<AnalyticsContextType>;
|
33
31
|
|
package/dist/index.js
CHANGED
@@ -1 +1,276 @@
|
|
1
|
-
import
|
1
|
+
import {
|
2
|
+
AnalyticsContext,
|
3
|
+
AnalyticsContextProvider_default
|
4
|
+
} from "./chunk-JQ6CXYV4.js";
|
5
|
+
|
6
|
+
// src/AnalyticsNext/index.tsx
|
7
|
+
import React2 from "react";
|
8
|
+
|
9
|
+
// src/AnalyticsNext/handle.tsx
|
10
|
+
import React, { useCallback, useEffect, useState } from "react";
|
11
|
+
|
12
|
+
// src/utils/services.ts
|
13
|
+
var cache;
|
14
|
+
var assign = (a, b) => {
|
15
|
+
Object.keys(b).forEach((key) => {
|
16
|
+
if (b[key] !== void 0)
|
17
|
+
a[key] = b[key];
|
18
|
+
});
|
19
|
+
return a;
|
20
|
+
};
|
21
|
+
var trackerService = async (endpoint, payload) => {
|
22
|
+
const fetchData = await fetch(endpoint, {
|
23
|
+
method: "POST",
|
24
|
+
body: JSON.stringify(payload),
|
25
|
+
headers: assign({ "Content-Type": "application/json" }, { ["x-tracker-cache"]: cache })
|
26
|
+
});
|
27
|
+
const response = await fetchData.json();
|
28
|
+
if (response.error) {
|
29
|
+
throw response.error;
|
30
|
+
} else {
|
31
|
+
return response;
|
32
|
+
}
|
33
|
+
};
|
34
|
+
|
35
|
+
// src/utils/index.ts
|
36
|
+
import Bowser from "bowser";
|
37
|
+
import * as FingerprintJS from "@fingerprintjs/fingerprintjs";
|
38
|
+
var createRequest = (endpoint, task) => {
|
39
|
+
return `${endpoint}/visitor/v1/${task}`;
|
40
|
+
};
|
41
|
+
var createRequestV2 = (endpoint, task) => {
|
42
|
+
return `${endpoint}/visitor/v2/${task}`;
|
43
|
+
};
|
44
|
+
var startTracker = async (endpoint, url, referer, user_agent) => {
|
45
|
+
const allow = sessionStorage.getItem("aesirx-analytics-allow");
|
46
|
+
if (allow === "0") {
|
47
|
+
return null;
|
48
|
+
}
|
49
|
+
const { location, document: document2 } = window;
|
50
|
+
const { pathname, search, origin } = location;
|
51
|
+
url = `${origin}${pathname}${search}`;
|
52
|
+
referer = referer ? location.protocol + "//" + location.host + referer : document2.referrer ? document2.referrer : "";
|
53
|
+
user_agent = window.navigator.userAgent;
|
54
|
+
const browser = Bowser.parse(window.navigator.userAgent);
|
55
|
+
const browser_name = browser?.browser?.name;
|
56
|
+
const browser_version = browser?.browser?.version ?? "0";
|
57
|
+
const lang = window.navigator["userLanguage"] || window.navigator.language;
|
58
|
+
const device = browser?.platform?.model ?? browser?.platform?.type;
|
59
|
+
const queryString = window.location.search;
|
60
|
+
const urlParams = new URLSearchParams(queryString);
|
61
|
+
const ip = "";
|
62
|
+
const fpPromise = FingerprintJS.load({ monitoring: false });
|
63
|
+
try {
|
64
|
+
const responseStart = fpPromise.then((fp) => fp.get()).then(async (result) => {
|
65
|
+
const fingerprint = result.visitorId;
|
66
|
+
const attributes = [];
|
67
|
+
for (const key of urlParams.keys()) {
|
68
|
+
if (key.startsWith("utm_")) {
|
69
|
+
urlParams.get(key) && attributes.push({ name: key, value: urlParams.get(key) });
|
70
|
+
}
|
71
|
+
}
|
72
|
+
return await trackerService(createRequestV2(endpoint, "start"), {
|
73
|
+
fingerprint,
|
74
|
+
url,
|
75
|
+
...referer && referer !== url && {
|
76
|
+
referer
|
77
|
+
},
|
78
|
+
user_agent,
|
79
|
+
ip,
|
80
|
+
browser_name,
|
81
|
+
browser_version,
|
82
|
+
lang,
|
83
|
+
device,
|
84
|
+
...attributes?.length && {
|
85
|
+
event_name: "visit",
|
86
|
+
event_type: "action",
|
87
|
+
attributes
|
88
|
+
}
|
89
|
+
});
|
90
|
+
});
|
91
|
+
return responseStart;
|
92
|
+
} catch (error) {
|
93
|
+
}
|
94
|
+
};
|
95
|
+
var trackEvent = async (endpoint, referer, data) => {
|
96
|
+
const allow = sessionStorage.getItem("aesirx-analytics-allow");
|
97
|
+
if (allow === "0") {
|
98
|
+
return null;
|
99
|
+
}
|
100
|
+
const { location, document: document2 } = window;
|
101
|
+
referer = referer ? location.protocol + "//" + location.host + referer : document2.referrer.split("?")[0];
|
102
|
+
const url = location.protocol + "//" + location.host + location.pathname;
|
103
|
+
const user_agent = window.navigator.userAgent;
|
104
|
+
const browser = Bowser.parse(window.navigator.userAgent);
|
105
|
+
const browser_name = browser?.browser?.name;
|
106
|
+
const browser_version = browser?.browser?.version ?? "0";
|
107
|
+
const lang = window.navigator["userLanguage"] || window.navigator.language;
|
108
|
+
const device = browser?.platform?.model ?? browser?.platform?.type;
|
109
|
+
const ip = "";
|
110
|
+
const fpPromise = FingerprintJS.load({ monitoring: false });
|
111
|
+
const body = fpPromise.then((fp) => fp.get()).then(async (result) => {
|
112
|
+
const fingerprint = result.visitorId;
|
113
|
+
return {
|
114
|
+
fingerprint,
|
115
|
+
url,
|
116
|
+
...referer !== "/" && referer && {
|
117
|
+
referer
|
118
|
+
},
|
119
|
+
user_agent,
|
120
|
+
ip,
|
121
|
+
browser_name,
|
122
|
+
browser_version,
|
123
|
+
lang,
|
124
|
+
device,
|
125
|
+
...data
|
126
|
+
};
|
127
|
+
});
|
128
|
+
const headers = { type: "application/json" };
|
129
|
+
const blob = new Blob([JSON.stringify(body)], headers);
|
130
|
+
const responseStart = navigator.sendBeacon(createRequestV2(endpoint, "start"), blob);
|
131
|
+
return responseStart;
|
132
|
+
};
|
133
|
+
var endTracker = (endPoint, event_uuid, visitor_uuid) => {
|
134
|
+
if (event_uuid && visitor_uuid) {
|
135
|
+
const body = {
|
136
|
+
event_uuid,
|
137
|
+
visitor_uuid
|
138
|
+
};
|
139
|
+
const headers = { type: "application/json" };
|
140
|
+
const blob = new Blob([JSON.stringify(body)], headers);
|
141
|
+
navigator.sendBeacon(createRequest(endPoint, "end"), blob);
|
142
|
+
}
|
143
|
+
};
|
144
|
+
var endTrackerVisibilityState = (endPoint) => {
|
145
|
+
document.addEventListener("visibilitychange", async () => {
|
146
|
+
if (document.visibilityState === "hidden") {
|
147
|
+
endTracker(endPoint, window["event_uuid"], window["visitor_uuid"]);
|
148
|
+
}
|
149
|
+
if (document.visibilityState === "visible") {
|
150
|
+
const response = await startTracker(endPoint);
|
151
|
+
window["event_uuid"] = response?.event_uuid;
|
152
|
+
}
|
153
|
+
});
|
154
|
+
window.addEventListener(
|
155
|
+
"pagehide",
|
156
|
+
(event) => {
|
157
|
+
if (event.persisted) {
|
158
|
+
endTracker(endPoint, window["event_uuid"], window["visitor_uuid"]);
|
159
|
+
}
|
160
|
+
},
|
161
|
+
false
|
162
|
+
);
|
163
|
+
};
|
164
|
+
|
165
|
+
// src/AnalyticsNext/handle.tsx
|
166
|
+
var AnalyticsHandle = ({ router, children }) => {
|
167
|
+
const AnalyticsStore = React.useContext(AnalyticsContext);
|
168
|
+
const endPoint = process.env.NEXT_PUBLIC_ENDPOINT_ANALYTICS_URL;
|
169
|
+
const [prevRoute, setPrevRoute] = useState("");
|
170
|
+
const handleStartTracker = useCallback(
|
171
|
+
async (prevRoute2) => {
|
172
|
+
const referer = prevRoute2 ? prevRoute2 : "";
|
173
|
+
window["referer"] = referer;
|
174
|
+
const responseStart = await startTracker(endPoint, "", referer);
|
175
|
+
responseStart?.event_uuid && AnalyticsStore.setEventID(responseStart.event_uuid);
|
176
|
+
responseStart?.visitor_uuid && AnalyticsStore.setUUID(responseStart.visitor_uuid);
|
177
|
+
},
|
178
|
+
[AnalyticsStore, endPoint]
|
179
|
+
);
|
180
|
+
useEffect(() => {
|
181
|
+
const init = async () => {
|
182
|
+
if (!AnalyticsStore.visitor_uuid) {
|
183
|
+
await handleStartTracker(router.asPath);
|
184
|
+
}
|
185
|
+
};
|
186
|
+
init();
|
187
|
+
}, [AnalyticsStore.visitor_uuid]);
|
188
|
+
useEffect(() => {
|
189
|
+
const handleRouteChange = async () => {
|
190
|
+
setPrevRoute(router.asPath);
|
191
|
+
if (AnalyticsStore.visitor_uuid) {
|
192
|
+
endTracker(endPoint, window["event_uuid"], AnalyticsStore.visitor_uuid);
|
193
|
+
await handleStartTracker(prevRoute);
|
194
|
+
}
|
195
|
+
};
|
196
|
+
router.events.on("routeChangeComplete", handleRouteChange);
|
197
|
+
return () => {
|
198
|
+
router.events.off("routeChangeComplete", handleRouteChange);
|
199
|
+
};
|
200
|
+
}, [router.events, router.asPath, router.query]);
|
201
|
+
useEffect(() => {
|
202
|
+
const init = async () => {
|
203
|
+
endTrackerVisibilityState(endPoint);
|
204
|
+
};
|
205
|
+
init();
|
206
|
+
}, []);
|
207
|
+
useEffect(() => {
|
208
|
+
const init = async () => {
|
209
|
+
window["event_uuid"] = AnalyticsStore.event_uuid;
|
210
|
+
window["visitor_uuid"] = AnalyticsStore.visitor_uuid;
|
211
|
+
};
|
212
|
+
init();
|
213
|
+
}, [AnalyticsStore.event_uuid, AnalyticsStore.visitor_uuid]);
|
214
|
+
return /* @__PURE__ */ React.createElement(React.Fragment, null, children);
|
215
|
+
};
|
216
|
+
var handle_default = AnalyticsHandle;
|
217
|
+
|
218
|
+
// src/AnalyticsNext/index.tsx
|
219
|
+
import dynamic from "next/dynamic";
|
220
|
+
var ConsentComponent = dynamic(() => import("./Consent-7KHERFGJ.js"), { ssr: false });
|
221
|
+
var AnalyticsNext = ({ router, children }) => {
|
222
|
+
return /* @__PURE__ */ React2.createElement(React2.Fragment, null, /* @__PURE__ */ React2.createElement(AnalyticsContextProvider_default, null, /* @__PURE__ */ React2.createElement(handle_default, { router }, children, /* @__PURE__ */ React2.createElement(ConsentComponent, { endpoint: process.env.NEXT_PUBLIC_ENDPOINT_ANALYTICS_URL }))));
|
223
|
+
};
|
224
|
+
var AnalyticsNext_default = AnalyticsNext;
|
225
|
+
|
226
|
+
// src/AnalyticsReact/index.tsx
|
227
|
+
import React4, { Suspense } from "react";
|
228
|
+
|
229
|
+
// src/AnalyticsReact/handle.tsx
|
230
|
+
import React3, { useEffect as useEffect2 } from "react";
|
231
|
+
var AnalyticsHandle2 = ({ location, history, children }) => {
|
232
|
+
const AnalyticsStore = React3.useContext(AnalyticsContext);
|
233
|
+
const endPoint = process.env.REACT_APP_ENDPOINT_ANALYTICS_URL;
|
234
|
+
useEffect2(() => {
|
235
|
+
const init = async () => {
|
236
|
+
if (!AnalyticsStore.visitor_uuid) {
|
237
|
+
const referer = location.pathname ? location.pathname : "";
|
238
|
+
window["referer"] = referer;
|
239
|
+
const responseStart = await startTracker(endPoint, "", referer);
|
240
|
+
responseStart.event_uuid && AnalyticsStore.setEventID(responseStart.event_uuid);
|
241
|
+
responseStart.visitor_uuid && AnalyticsStore.setUUID(responseStart.visitor_uuid);
|
242
|
+
} else {
|
243
|
+
endTracker(endPoint, window["event_uuid"], AnalyticsStore.visitor_uuid);
|
244
|
+
}
|
245
|
+
};
|
246
|
+
init();
|
247
|
+
}, [location.pathname, history]);
|
248
|
+
useEffect2(() => {
|
249
|
+
const init = async () => {
|
250
|
+
endTrackerVisibilityState(endPoint);
|
251
|
+
};
|
252
|
+
init();
|
253
|
+
}, []);
|
254
|
+
useEffect2(() => {
|
255
|
+
const init = async () => {
|
256
|
+
window["event_uuid"] = AnalyticsStore.event_uuid;
|
257
|
+
window["visitor_uuid"] = AnalyticsStore.visitor_uuid;
|
258
|
+
};
|
259
|
+
init();
|
260
|
+
}, [AnalyticsStore.event_uuid, AnalyticsStore.visitor_uuid]);
|
261
|
+
return /* @__PURE__ */ React3.createElement(React3.Fragment, null, children);
|
262
|
+
};
|
263
|
+
var handle_default2 = AnalyticsHandle2;
|
264
|
+
|
265
|
+
// src/AnalyticsReact/index.tsx
|
266
|
+
var ConsentComponent2 = React4.lazy(() => import("./Consent-7KHERFGJ.js"));
|
267
|
+
var AnalyticsReact = ({ location, history, children }) => {
|
268
|
+
return /* @__PURE__ */ React4.createElement(AnalyticsContextProvider_default, null, /* @__PURE__ */ React4.createElement(handle_default2, { location, history }, children, /* @__PURE__ */ React4.createElement(Suspense, { fallback: /* @__PURE__ */ React4.createElement(React4.Fragment, null) }, /* @__PURE__ */ React4.createElement(ConsentComponent2, { endpoint: process.env.REACT_APP_ENDPOINT_ANALYTICS_URL }))));
|
269
|
+
};
|
270
|
+
var AnalyticsReact_default = AnalyticsReact;
|
271
|
+
export {
|
272
|
+
AnalyticsContext,
|
273
|
+
AnalyticsNext_default as AnalyticsNext,
|
274
|
+
AnalyticsReact_default as AnalyticsReact,
|
275
|
+
trackEvent
|
276
|
+
};
|
package/package.json
CHANGED
@@ -1,15 +1,18 @@
|
|
1
1
|
{
|
2
2
|
"name": "aesirx-analytics",
|
3
|
-
"version": "
|
3
|
+
"version": "2.0.0",
|
4
4
|
"license": "GPL-3.0-only",
|
5
5
|
"author": "AesirX",
|
6
6
|
"repository": "https://gitlab.redweb.dk/aesirx/analytics",
|
7
7
|
"main": "dist/index.js",
|
8
8
|
"module": "dist/index.js",
|
9
|
+
"types": "dist/index.d.ts",
|
9
10
|
"type": "module",
|
10
11
|
"dependencies": {
|
11
12
|
"@concordium/browser-wallet-api-helpers": "^2",
|
12
13
|
"@concordium/web-sdk": "^6",
|
14
|
+
"@fingerprintjs/fingerprintjs": "^3.4.2",
|
15
|
+
"aesirx-sso": "^1.1.1",
|
13
16
|
"axios": "^1.4.0",
|
14
17
|
"bootstrap": "^5.3.0",
|
15
18
|
"bowser": "^2.11.0",
|
@@ -23,7 +26,7 @@
|
|
23
26
|
"react-toastify": "^9.1.3"
|
24
27
|
},
|
25
28
|
"scripts": {
|
26
|
-
"dev": "
|
29
|
+
"dev": "tsup --watch --onSuccess 'yalc push --changed --no-scripts'",
|
27
30
|
"build": "tsup",
|
28
31
|
"lint": "eslint --fix \"src/**/\"",
|
29
32
|
"lint:check": "eslint \"src/**/\"",
|
@@ -71,7 +74,6 @@
|
|
71
74
|
"jest-environment-jsdom": "^29.4.3",
|
72
75
|
"node-fetch": "^3.3.1",
|
73
76
|
"prettier": "^2.8.4",
|
74
|
-
"terser": "^5.19.0",
|
75
77
|
"ts-jest": "^29.0.5",
|
76
78
|
"tsup": "^6.7.0",
|
77
79
|
"typescript": "^5.0.3"
|