@armco/analytics 0.2.8 → 0.2.9
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/analytics.d.ts +1 -1
- package/analytics.js +96 -62
- package/flush.js +13 -13
- package/global-modules.d.ts +4 -4
- package/location.js +11 -9
- package/package.json +1 -6
- package/session.js +8 -8
- package/tsconfig.prod.tsbuildinfo +1 -0
package/analytics.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { User, Event, ConfigType } from "./index.interface";
|
|
|
3
3
|
declare let enabled: boolean | null;
|
|
4
4
|
declare function loadConfiguration(): Promise<any>;
|
|
5
5
|
declare function getEnvironment(): string;
|
|
6
|
-
declare function getEnvironmentType():
|
|
6
|
+
declare function getEnvironmentType(): "browser" | "node" | "unknown";
|
|
7
7
|
export declare function sendBulkData(data: Event[], callback?: Function): Promise<void>;
|
|
8
8
|
declare function trackEvent(event: string | Event, data?: any): void;
|
|
9
9
|
declare function trackPageView(pageName: string, data?: {
|
package/analytics.js
CHANGED
|
@@ -7,17 +7,20 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import * as fs from
|
|
11
|
-
import * as path from
|
|
12
|
-
import $ from "jquery";
|
|
10
|
+
import * as fs from "fs";
|
|
11
|
+
import * as path from "path";
|
|
13
12
|
import { v4 as uuidv4 } from "uuid";
|
|
14
13
|
import { ADD, ARMCO_SERVER } from "./constants";
|
|
15
14
|
import { startSession, getSessionId, terminateSession } from "./session";
|
|
16
15
|
import { ipLookup, success, error, localTimeRegion } from "./location";
|
|
17
|
-
import { isArClient } from
|
|
18
|
-
import { hookFlushHandlers, queueEvent } from
|
|
19
|
-
const tsConfigPath = getEnvironmentType() === "node"
|
|
20
|
-
|
|
16
|
+
import { isArClient } from "./helper";
|
|
17
|
+
import { hookFlushHandlers, queueEvent } from "./flush";
|
|
18
|
+
const tsConfigPath = getEnvironmentType() === "node"
|
|
19
|
+
? path.resolve(process.cwd(), "tsconfig.json")
|
|
20
|
+
: "../../../../tsConfig.json";
|
|
21
|
+
const packageJsonPath = getEnvironmentType() === "node"
|
|
22
|
+
? path.resolve(process.cwd(), "package.json")
|
|
23
|
+
: "../../../../package.json";
|
|
21
24
|
let ar_anonymous_id;
|
|
22
25
|
let user = null;
|
|
23
26
|
let apiKey = null;
|
|
@@ -34,7 +37,9 @@ let CONFIG;
|
|
|
34
37
|
let environment;
|
|
35
38
|
function isTypeScriptProject() {
|
|
36
39
|
return __awaiter(this, void 0, void 0, function* () {
|
|
37
|
-
if (fs &&
|
|
40
|
+
if (fs &&
|
|
41
|
+
"existsSync" in fs &&
|
|
42
|
+
!(fs.existsSync(tsConfigPath) && fs.existsSync(packageJsonPath))) {
|
|
38
43
|
return false;
|
|
39
44
|
}
|
|
40
45
|
let tsConfig, packageJson;
|
|
@@ -45,7 +50,7 @@ function isTypeScriptProject() {
|
|
|
45
50
|
}
|
|
46
51
|
}
|
|
47
52
|
catch (error) {
|
|
48
|
-
console.error(
|
|
53
|
+
console.error("Error loading tsconfig.json:", error);
|
|
49
54
|
}
|
|
50
55
|
try {
|
|
51
56
|
packageJson = yield import(tsConfigPath);
|
|
@@ -56,7 +61,7 @@ function isTypeScriptProject() {
|
|
|
56
61
|
}
|
|
57
62
|
}
|
|
58
63
|
catch (error) {
|
|
59
|
-
console.error(
|
|
64
|
+
console.error("Error loading tsconfig.json:", error);
|
|
60
65
|
}
|
|
61
66
|
if (packageJson &&
|
|
62
67
|
packageJson.devDependencies &&
|
|
@@ -92,25 +97,25 @@ function loadConfiguration() {
|
|
|
92
97
|
});
|
|
93
98
|
}
|
|
94
99
|
function getEnvironment() {
|
|
95
|
-
if (typeof process !==
|
|
100
|
+
if (typeof process !== "undefined" && process.env && process.env.NODE_ENV) {
|
|
96
101
|
return process.env.NODE_ENV;
|
|
97
102
|
}
|
|
98
103
|
if (import.meta.env && import.meta.env.MODE) {
|
|
99
104
|
return import.meta.env.MODE;
|
|
100
105
|
}
|
|
101
|
-
return
|
|
106
|
+
return "development";
|
|
102
107
|
}
|
|
103
108
|
function getEnvironmentType() {
|
|
104
|
-
if (typeof window !==
|
|
105
|
-
return
|
|
109
|
+
if (typeof window !== "undefined" && typeof window.document !== "undefined") {
|
|
110
|
+
return "browser";
|
|
106
111
|
}
|
|
107
|
-
else if (typeof process !==
|
|
112
|
+
else if (typeof process !== "undefined" &&
|
|
108
113
|
process.versions != null &&
|
|
109
114
|
process.versions.node != null) {
|
|
110
|
-
return
|
|
115
|
+
return "node";
|
|
111
116
|
}
|
|
112
117
|
else {
|
|
113
|
-
return
|
|
118
|
+
return "unknown";
|
|
114
119
|
}
|
|
115
120
|
}
|
|
116
121
|
function getHostProjectName() {
|
|
@@ -139,7 +144,7 @@ function enrichEventData(data) {
|
|
|
139
144
|
address && !data.address && (data.address = address);
|
|
140
145
|
coordinates && !data.coordinates && (data.coordinates = coordinates);
|
|
141
146
|
!data.timestamp && (data.timestamp = new Date());
|
|
142
|
-
!data.userId && (data.userId =
|
|
147
|
+
!data.userId && (data.userId = user ? user.email : ar_anonymous_id);
|
|
143
148
|
!data.email && user && user.email && (data.email = user.email);
|
|
144
149
|
user && (data.user = user);
|
|
145
150
|
}
|
|
@@ -157,30 +162,37 @@ function sendAnalyticsData(data) {
|
|
|
157
162
|
return __awaiter(this, void 0, void 0, function* () {
|
|
158
163
|
try {
|
|
159
164
|
if (!apiKey && !analyticsLogEndpoint) {
|
|
160
|
-
console.error(
|
|
165
|
+
console.error("Neither of API key and Analytics server configured. Data not sent.");
|
|
161
166
|
return;
|
|
162
167
|
}
|
|
163
168
|
const options = {
|
|
164
169
|
method: "POST",
|
|
165
170
|
headers: {
|
|
166
|
-
|
|
171
|
+
"Content-Type": "application/json",
|
|
167
172
|
},
|
|
168
173
|
body: JSON.stringify({ event: data }),
|
|
169
174
|
};
|
|
170
175
|
if (apiKey) {
|
|
171
176
|
options.headers.Authorization = `Bearer ${apiKey}`;
|
|
172
177
|
}
|
|
173
|
-
const logEndpoint = apiKey
|
|
178
|
+
const logEndpoint = apiKey
|
|
179
|
+
? ARMCO_SERVER + ADD
|
|
180
|
+
: analyticsLogEndpoint;
|
|
174
181
|
try {
|
|
175
182
|
const response = yield fetch(logEndpoint, options);
|
|
176
|
-
|
|
183
|
+
if (response.status === 200) {
|
|
184
|
+
console.log("Analytics data sent to server:", logEndpoint, data, response.status, response.statusText);
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
console.warn("Failed to send analytics payload to:", logEndpoint, data, response.status, response.statusText);
|
|
188
|
+
}
|
|
177
189
|
}
|
|
178
190
|
catch (e) {
|
|
179
191
|
console.warn("Failed attempt to log event");
|
|
180
192
|
}
|
|
181
193
|
}
|
|
182
194
|
catch (error) {
|
|
183
|
-
console.error(
|
|
195
|
+
console.error("Failed to send analytics data:", error);
|
|
184
196
|
}
|
|
185
197
|
});
|
|
186
198
|
}
|
|
@@ -188,25 +200,27 @@ function tagEvents(email) {
|
|
|
188
200
|
return __awaiter(this, void 0, void 0, function* () {
|
|
189
201
|
try {
|
|
190
202
|
if (!apiKey && !analyticsTagEndpoint) {
|
|
191
|
-
console.error(
|
|
203
|
+
console.error("Neither of API key and Analytics server configured. Tagging won't be attempted.");
|
|
192
204
|
return;
|
|
193
205
|
}
|
|
194
206
|
const options = {
|
|
195
207
|
method: "POST",
|
|
196
208
|
headers: {
|
|
197
|
-
|
|
209
|
+
"Content-Type": "application/json",
|
|
198
210
|
},
|
|
199
211
|
body: { event: JSON.stringify({ email, anonymousId: ar_anonymous_id }) },
|
|
200
212
|
};
|
|
201
213
|
if (apiKey) {
|
|
202
214
|
options.headers.Authorization = `Bearer ${apiKey}`;
|
|
203
215
|
}
|
|
204
|
-
const tagEndpoint = apiKey
|
|
216
|
+
const tagEndpoint = apiKey
|
|
217
|
+
? ARMCO_SERVER + ADD
|
|
218
|
+
: analyticsTagEndpoint;
|
|
205
219
|
const response = yield fetch(tagEndpoint, options);
|
|
206
|
-
console.log(
|
|
220
|
+
console.log("Analytics data sent to server:", tagEndpoint, response.status, response.statusText);
|
|
207
221
|
}
|
|
208
222
|
catch (error) {
|
|
209
|
-
console.error(
|
|
223
|
+
console.error("Failed to send analytics data:", error);
|
|
210
224
|
}
|
|
211
225
|
});
|
|
212
226
|
}
|
|
@@ -227,28 +241,30 @@ function trackEvent(event, data) {
|
|
|
227
241
|
return;
|
|
228
242
|
}
|
|
229
243
|
enrichEventData(data);
|
|
230
|
-
(CONFIG === null || CONFIG === void 0 ? void 0 : CONFIG.submissionStrategy) === "DEFER"
|
|
244
|
+
(CONFIG === null || CONFIG === void 0 ? void 0 : CONFIG.submissionStrategy) === "DEFER"
|
|
245
|
+
? queueEvent(data)
|
|
246
|
+
: sendAnalyticsData(data);
|
|
231
247
|
}
|
|
232
248
|
else {
|
|
233
|
-
console.log(
|
|
249
|
+
console.log("Analytics disabled or user not identified. Event data not sent.");
|
|
234
250
|
}
|
|
235
251
|
}
|
|
236
252
|
function trackPageView(pageName, data) {
|
|
237
253
|
const pageViewEvent = Object.assign({ eventType: "Page View", pageName }, data);
|
|
238
|
-
trackEvent(
|
|
254
|
+
trackEvent("Page View", pageViewEvent);
|
|
239
255
|
}
|
|
240
256
|
function trackError(errorMessage) {
|
|
241
257
|
const errorEvent = {
|
|
242
|
-
eventType:
|
|
258
|
+
eventType: "Error",
|
|
243
259
|
timestamp: new Date(),
|
|
244
260
|
errorMessage,
|
|
245
261
|
};
|
|
246
262
|
trackEvent(errorEvent);
|
|
247
263
|
}
|
|
248
264
|
function isEnabled() {
|
|
249
|
-
if (typeof navigator !==
|
|
265
|
+
if (typeof navigator !== "undefined" && "doNotTrack" in navigator) {
|
|
250
266
|
const doNotTrackValue = navigator.doNotTrack;
|
|
251
|
-
if (doNotTrackValue ===
|
|
267
|
+
if (doNotTrackValue === "1" || doNotTrackValue === "yes") {
|
|
252
268
|
console.warn("[ANALYTICS] Tracking disabled in client, events will not be logged!");
|
|
253
269
|
return isArClient(hostProjectName) || false;
|
|
254
270
|
}
|
|
@@ -269,7 +285,10 @@ function populateLocationDetails() {
|
|
|
269
285
|
}
|
|
270
286
|
else {
|
|
271
287
|
navigator.geolocation.getCurrentPosition((position) => {
|
|
272
|
-
coordinates = {
|
|
288
|
+
coordinates = {
|
|
289
|
+
latitude: position.coords.latitude,
|
|
290
|
+
longitude: position.coords.longitude,
|
|
291
|
+
};
|
|
273
292
|
success(position, (reverseGeocodingResponse) => {
|
|
274
293
|
address = reverseGeocodingResponse.results[0].formatted_address;
|
|
275
294
|
});
|
|
@@ -301,12 +320,12 @@ const trackedItems = [
|
|
|
301
320
|
"[role='menuitem']",
|
|
302
321
|
"[role='menuitemcheckbox']",
|
|
303
322
|
"[role='menuitemradio']",
|
|
304
|
-
"[data-track='true']"
|
|
323
|
+
"[data-track='true']",
|
|
305
324
|
];
|
|
306
325
|
function isClickable(element) {
|
|
307
|
-
return element.matches(trackedItems.join(", ")) ||
|
|
326
|
+
return (element.matches(trackedItems.join(", ")) ||
|
|
308
327
|
element.onclick != null ||
|
|
309
|
-
window.getComputedStyle(element).cursor
|
|
328
|
+
window.getComputedStyle(element).cursor === "pointer");
|
|
310
329
|
}
|
|
311
330
|
function handleTrackedItemClick(e) {
|
|
312
331
|
var _a;
|
|
@@ -314,14 +333,16 @@ function handleTrackedItemClick(e) {
|
|
|
314
333
|
if (isClickable(clickedElement)) {
|
|
315
334
|
const dataAttributes = Object.assign({}, clickedElement.dataset);
|
|
316
335
|
const id = clickedElement.id || null;
|
|
317
|
-
const name = clickedElement.getAttribute(
|
|
336
|
+
const name = clickedElement.getAttribute("name") || null;
|
|
318
337
|
const classes = Array.from(clickedElement.classList);
|
|
319
338
|
const elementType = clickedElement.tagName.toLowerCase();
|
|
320
339
|
const textContent = clickedElement.textContent;
|
|
321
|
-
const href =
|
|
322
|
-
|
|
340
|
+
const href = "href" in clickedElement
|
|
341
|
+
? clickedElement.href
|
|
342
|
+
: null;
|
|
343
|
+
const role = clickedElement.getAttribute("role") || null;
|
|
323
344
|
const parentElementId = ((_a = clickedElement.parentElement) === null || _a === void 0 ? void 0 : _a.id) || null;
|
|
324
|
-
const value =
|
|
345
|
+
const value = "value" in clickedElement && clickedElement.value
|
|
325
346
|
? clickedElement.value
|
|
326
347
|
: null;
|
|
327
348
|
const mergedData = Object.assign(Object.assign({}, dataAttributes), { id,
|
|
@@ -336,36 +357,46 @@ function handleTrackedItemClick(e) {
|
|
|
336
357
|
}
|
|
337
358
|
function hookTrackers() {
|
|
338
359
|
if (environment === "browser") {
|
|
339
|
-
const TRACK_EVENTS = (CONFIG === null || CONFIG === void 0 ? void 0 : CONFIG.trackEvents) || [
|
|
360
|
+
const TRACK_EVENTS = (CONFIG === null || CONFIG === void 0 ? void 0 : CONFIG.trackEvents) || [
|
|
361
|
+
"click",
|
|
362
|
+
"submit",
|
|
363
|
+
"select-change",
|
|
364
|
+
];
|
|
340
365
|
if (TRACK_EVENTS.indexOf("click") > -1) {
|
|
341
366
|
console.log("[ANALYTICS] Attaching Click handlers");
|
|
342
|
-
const clickables = Array.from(document.querySelectorAll(
|
|
343
|
-
console.log("[ANALYTICS] Found " +
|
|
367
|
+
const clickables = Array.from(document.querySelectorAll("*"));
|
|
368
|
+
console.log("[ANALYTICS] Found " +
|
|
369
|
+
clickables.length +
|
|
370
|
+
" items that can be clicked!");
|
|
344
371
|
console.log("[ANALYTICS] Dynamically added elements will be added to this list.");
|
|
345
372
|
document.addEventListener("click", handleTrackedItemClick);
|
|
346
373
|
console.log("[ANALYTICS] Click handlers Attached");
|
|
347
374
|
}
|
|
348
375
|
if (TRACK_EVENTS.indexOf("submit") > -1) {
|
|
349
376
|
console.log("[ANALYTICS] Attaching Submit handlers");
|
|
350
|
-
document.addEventListener(
|
|
377
|
+
document.addEventListener("submit", (event) => {
|
|
351
378
|
const formElement = event.target;
|
|
352
|
-
trackEvent(
|
|
379
|
+
trackEvent("SUBMIT", {
|
|
353
380
|
submit: {
|
|
354
381
|
formId: formElement.id,
|
|
355
|
-
formData: new FormData(formElement)
|
|
356
|
-
}
|
|
382
|
+
formData: new FormData(formElement),
|
|
383
|
+
},
|
|
357
384
|
});
|
|
358
385
|
});
|
|
359
386
|
console.log("[ANALYTICS] Submit handlers attached");
|
|
360
387
|
}
|
|
361
388
|
if (TRACK_EVENTS.indexOf("select-change") > -1) {
|
|
362
389
|
console.log("[ANALYTICS] Attaching Select OnChange handlers");
|
|
363
|
-
document.addEventListener(
|
|
390
|
+
document.addEventListener("change", (event) => {
|
|
364
391
|
const target = event.target;
|
|
365
|
-
if (target.tagName ===
|
|
392
|
+
if (target.tagName === "SELECT") {
|
|
366
393
|
const selectedOptionValue = target.value;
|
|
367
|
-
const selectedOptionLabel = target
|
|
368
|
-
|
|
394
|
+
const selectedOptionLabel = target
|
|
395
|
+
.selectedOptions[0].label;
|
|
396
|
+
trackEvent("SELECT_CHANGE", {
|
|
397
|
+
value: selectedOptionValue,
|
|
398
|
+
label: selectedOptionLabel,
|
|
399
|
+
});
|
|
369
400
|
}
|
|
370
401
|
});
|
|
371
402
|
console.log("[ANALYTICS] Select OnChange handlers attached");
|
|
@@ -391,6 +422,7 @@ function sendHostProjectName() {
|
|
|
391
422
|
}
|
|
392
423
|
}
|
|
393
424
|
function showTrackingPopup() {
|
|
425
|
+
var _a, _b;
|
|
394
426
|
const popupContent = `
|
|
395
427
|
<div class="tracking-popup" style="position: fixed;
|
|
396
428
|
width: 50%;
|
|
@@ -414,13 +446,16 @@ function showTrackingPopup() {
|
|
|
414
446
|
>Got it!</a>
|
|
415
447
|
</div>
|
|
416
448
|
`;
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
449
|
+
document.body.insertAdjacentHTML("beforeend", popupContent);
|
|
450
|
+
(_a = document.querySelector(".btn-accept")) === null || _a === void 0 ? void 0 : _a.addEventListener("click", function () {
|
|
451
|
+
var _a;
|
|
452
|
+
(_a = document.querySelector(".tracking-popup")) === null || _a === void 0 ? void 0 : _a.remove();
|
|
420
453
|
});
|
|
421
|
-
|
|
454
|
+
(_b = document
|
|
455
|
+
.querySelector(".btn-decline")) === null || _b === void 0 ? void 0 : _b.addEventListener("click", function () {
|
|
456
|
+
var _a;
|
|
422
457
|
enableTracking(false);
|
|
423
|
-
|
|
458
|
+
(_a = document.querySelector(".tracking-popup")) === null || _a === void 0 ? void 0 : _a.remove();
|
|
424
459
|
});
|
|
425
460
|
}
|
|
426
461
|
function loadAnalytics(config) {
|
|
@@ -439,7 +474,7 @@ function loadAnalytics(config) {
|
|
|
439
474
|
CONFIG.showPopUp && showTrackingPopup();
|
|
440
475
|
console.log("[ANALYTICS] Hook Event Trackers");
|
|
441
476
|
hookTrackers();
|
|
442
|
-
console.log(
|
|
477
|
+
console.log('[ANALYTICS] Hook Handlers to flush events (use when submissionStrategy is configured as "DEFER"');
|
|
443
478
|
hookFlushHandlers(CONFIG.submissionStrategy);
|
|
444
479
|
console.log("[ANALYTICS] Find User Location Details");
|
|
445
480
|
populateLocationDetails();
|
|
@@ -447,7 +482,7 @@ function loadAnalytics(config) {
|
|
|
447
482
|
const anonId = generateAnonymousId();
|
|
448
483
|
console.log("Tracking User as", anonId);
|
|
449
484
|
startSession();
|
|
450
|
-
window.addEventListener(
|
|
485
|
+
window.addEventListener("load", function () {
|
|
451
486
|
console.log("[ANALYTICS] Logging page load");
|
|
452
487
|
trackEvent("PAGE");
|
|
453
488
|
});
|
|
@@ -478,8 +513,7 @@ function init(config) {
|
|
|
478
513
|
loadAnalytics(config);
|
|
479
514
|
}
|
|
480
515
|
else {
|
|
481
|
-
loadConfiguration()
|
|
482
|
-
.then(config => {
|
|
516
|
+
loadConfiguration().then((config) => {
|
|
483
517
|
loadAnalytics(config);
|
|
484
518
|
});
|
|
485
519
|
}
|
package/flush.js
CHANGED
|
@@ -13,20 +13,20 @@ function flushEvents() {
|
|
|
13
13
|
events = [];
|
|
14
14
|
});
|
|
15
15
|
}
|
|
16
|
+
function handleBeforeUnload() {
|
|
17
|
+
if (events.length > 0) {
|
|
18
|
+
flushEvents();
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function handleVisibilityChange() {
|
|
22
|
+
if (document.visibilityState === "hidden" && events.length > 0) {
|
|
23
|
+
flushEvents();
|
|
24
|
+
}
|
|
25
|
+
}
|
|
16
26
|
export function hookFlushHandlers(submissionStrategy = "ONEVENT") {
|
|
17
|
-
if (typeof window !==
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
flushEvents();
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
function handleVisibilityChange() {
|
|
24
|
-
if (document.visibilityState === 'hidden' && events.length > 0) {
|
|
25
|
-
flushEvents();
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
window.addEventListener('beforeunload', handleBeforeUnload);
|
|
29
|
-
document.addEventListener('visibilitychange', handleVisibilityChange);
|
|
27
|
+
if (typeof window !== "undefined" && submissionStrategy === "DEFER") {
|
|
28
|
+
window.addEventListener("beforeunload", handleBeforeUnload);
|
|
29
|
+
document.addEventListener("visibilitychange", handleVisibilityChange);
|
|
30
30
|
setInterval(() => {
|
|
31
31
|
if (events.length > 0) {
|
|
32
32
|
flushEvents();
|
package/global-modules.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import analytics from
|
|
1
|
+
import analytics from "./index"
|
|
2
2
|
|
|
3
3
|
declare global {
|
|
4
4
|
namespace NodeJS {
|
|
5
5
|
interface Global {
|
|
6
|
-
analytics: analytics
|
|
6
|
+
analytics: analytics
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
9
|
interface Window {
|
|
10
|
-
analytics: analytics
|
|
10
|
+
analytics: analytics
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
export {}
|
|
14
|
+
export {}
|
package/location.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import jstz from "jstz";
|
|
2
2
|
export function ipLookup() {
|
|
3
|
-
fetch(
|
|
4
|
-
.then(res => res.json())
|
|
5
|
-
.then(response => {
|
|
3
|
+
fetch("https://extreme-ip-lookup.com/json/")
|
|
4
|
+
.then((res) => res.json())
|
|
5
|
+
.then((response) => {
|
|
6
6
|
fallbackProcess(response);
|
|
7
7
|
})
|
|
8
8
|
.catch(() => {
|
|
9
|
-
console.log(
|
|
9
|
+
console.log("We could not find your location");
|
|
10
10
|
});
|
|
11
11
|
}
|
|
12
12
|
export function success(position, callback) {
|
|
@@ -20,11 +20,11 @@ export function error() {
|
|
|
20
20
|
function reverseGeocodingWithGoogle(latitude, longitude, callback) {
|
|
21
21
|
fetch(`https://maps.googleapis.com/maps/api/geocode/json?
|
|
22
22
|
latlng=${latitude},${longitude}&key={GOOGLE_MAP_KEY}`)
|
|
23
|
-
.then(res => res.json())
|
|
24
|
-
.then(response => {
|
|
23
|
+
.then((res) => res.json())
|
|
24
|
+
.then((response) => {
|
|
25
25
|
callback ? callback(response) : processUserData(response);
|
|
26
26
|
})
|
|
27
|
-
.catch(status => {
|
|
27
|
+
.catch((status) => {
|
|
28
28
|
ipLookup();
|
|
29
29
|
});
|
|
30
30
|
}
|
|
@@ -32,9 +32,11 @@ function processUserData(response) {
|
|
|
32
32
|
console.log(response.results[0].formatted_address);
|
|
33
33
|
}
|
|
34
34
|
function fallbackProcess(response) {
|
|
35
|
-
const address = document.querySelector(
|
|
35
|
+
const address = document.querySelector(".address");
|
|
36
36
|
address.innerText = `${response.city}, ${response.country}`;
|
|
37
37
|
}
|
|
38
38
|
const localTimeRegion = jstz.determine().name();
|
|
39
|
-
const localTime = new Date().toLocaleString("en-US", {
|
|
39
|
+
const localTime = new Date().toLocaleString("en-US", {
|
|
40
|
+
timeZone: localTimeRegion,
|
|
41
|
+
});
|
|
40
42
|
export { localTimeRegion, localTime };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@armco/analytics",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.9",
|
|
4
4
|
"description": "Browser Based Analytics interceptor for configured events",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -27,13 +27,8 @@
|
|
|
27
27
|
],
|
|
28
28
|
"devDependencies": {},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"jet-logger": "^1.3.1",
|
|
31
|
-
"jquery": "^3.7.0",
|
|
32
30
|
"js-cookie": "^3.0.5",
|
|
33
31
|
"jstz": "^2.1.1",
|
|
34
32
|
"uuid": "^9.0.0"
|
|
35
|
-
},
|
|
36
|
-
"peerDependencies": {
|
|
37
|
-
"jquery": "^3.7.0"
|
|
38
33
|
}
|
|
39
34
|
}
|
package/session.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import Cookies from
|
|
1
|
+
import Cookies from "js-cookie";
|
|
2
2
|
import { v4 as uuidv4 } from "uuid";
|
|
3
|
-
const SESSION_COOKIE_NAME =
|
|
3
|
+
const SESSION_COOKIE_NAME = "ar-session-id";
|
|
4
4
|
const SESSION_EXPIRATION_TIME = 30;
|
|
5
5
|
let localStorageTimeout;
|
|
6
6
|
function generateSessionId() {
|
|
@@ -9,11 +9,11 @@ function generateSessionId() {
|
|
|
9
9
|
export function startSession() {
|
|
10
10
|
const sessionId = generateSessionId();
|
|
11
11
|
const expirationDate = new Date();
|
|
12
|
-
let tabId = sessionStorage.getItem(
|
|
12
|
+
let tabId = sessionStorage.getItem("tabId");
|
|
13
13
|
if (!tabId) {
|
|
14
14
|
const timestamp = expirationDate.getTime();
|
|
15
15
|
tabId = `${uuidv4()}-${timestamp}`;
|
|
16
|
-
sessionStorage.setItem(
|
|
16
|
+
sessionStorage.setItem("tabId", tabId);
|
|
17
17
|
}
|
|
18
18
|
const cookieName = `${SESSION_COOKIE_NAME}-${tabId}`;
|
|
19
19
|
refreshSessionId(sessionId, cookieName);
|
|
@@ -32,7 +32,7 @@ function refreshSessionId(sessionId, cookieName) {
|
|
|
32
32
|
}
|
|
33
33
|
export function getSessionId() {
|
|
34
34
|
let sessionId;
|
|
35
|
-
const tabId = sessionStorage.getItem(
|
|
35
|
+
const tabId = sessionStorage.getItem("tabId");
|
|
36
36
|
if (!tabId) {
|
|
37
37
|
return startSession();
|
|
38
38
|
}
|
|
@@ -48,7 +48,7 @@ export function getSessionId() {
|
|
|
48
48
|
return sessionId;
|
|
49
49
|
}
|
|
50
50
|
export function extendSession() {
|
|
51
|
-
const tabId = sessionStorage.getItem(
|
|
51
|
+
const tabId = sessionStorage.getItem("tabId");
|
|
52
52
|
if (!tabId) {
|
|
53
53
|
return;
|
|
54
54
|
}
|
|
@@ -62,12 +62,12 @@ export function extendSession() {
|
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
export function terminateSession() {
|
|
65
|
-
const tabId = sessionStorage.getItem(
|
|
65
|
+
const tabId = sessionStorage.getItem("tabId");
|
|
66
66
|
if (!tabId) {
|
|
67
67
|
return;
|
|
68
68
|
}
|
|
69
69
|
const cookieName = `${SESSION_COOKIE_NAME}-${tabId}`;
|
|
70
|
-
if (typeof window !==
|
|
70
|
+
if (typeof window !== "undefined" && window.Cookies) {
|
|
71
71
|
Cookies.remove(cookieName);
|
|
72
72
|
}
|
|
73
73
|
else {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"root":["../analytics.ts","../constants.ts","../flush.ts","../global-modules.d.ts","../helper.ts","../index.interface.ts","../index.ts","../location.ts","../session.ts"],"version":"5.8.2"}
|