@reservamos/browser-analytics 1.1.2-alpha.0 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +5 -0
- package/dist/browser-analytics.cjs +1 -1
- package/dist/browser-analytics.cjs.map +1 -1
- package/dist/browser-analytics.esm.js +23 -4
- package/dist/browser-analytics.esm.js.map +1 -1
- package/dist/browser-analytics.iife.js +1 -1
- package/dist/browser-analytics.iife.js.map +1 -1
- package/package.json +1 -1
- package/src/services/geolocation.test.ts +8 -0
- package/src/services/geolocation.ts +29 -0
- package/src/track.ts +4 -0
package/package.json
CHANGED
|
@@ -103,9 +103,11 @@ describe('geolocationService', () => {
|
|
|
103
103
|
'Geolocation Accuracy': 12,
|
|
104
104
|
latitude: 19.4326,
|
|
105
105
|
longitude: -99.1332,
|
|
106
|
+
geolocation_source: 'browser',
|
|
106
107
|
};
|
|
107
108
|
|
|
108
109
|
expect(coordinates).toEqual({ lat: 19.4326, long: -99.1332 });
|
|
110
|
+
expect(geolocationService.getGeolocationSource()).toBe('browser');
|
|
109
111
|
expect(trackCustomEventMock).toHaveBeenCalledWith(
|
|
110
112
|
'Geolocation Requested',
|
|
111
113
|
expectedProperties,
|
|
@@ -115,6 +117,7 @@ describe('geolocationService', () => {
|
|
|
115
117
|
'Geolocation Status': 'granted',
|
|
116
118
|
latitude: 19.4326,
|
|
117
119
|
longitude: -99.1332,
|
|
120
|
+
geolocation_source: 'browser',
|
|
118
121
|
});
|
|
119
122
|
|
|
120
123
|
vi.useRealTimers();
|
|
@@ -142,6 +145,7 @@ describe('geolocationService', () => {
|
|
|
142
145
|
};
|
|
143
146
|
|
|
144
147
|
expect(coordinates).toBeNull();
|
|
148
|
+
expect(geolocationService.getGeolocationSource()).toBeNull();
|
|
145
149
|
expect(trackCustomEventMock).toHaveBeenCalledWith(
|
|
146
150
|
'Geolocation Requested',
|
|
147
151
|
expectedProperties,
|
|
@@ -219,6 +223,7 @@ describe('geolocationService', () => {
|
|
|
219
223
|
'Geolocation Accuracy': 25,
|
|
220
224
|
latitude: 19.4326,
|
|
221
225
|
longitude: -99.1332,
|
|
226
|
+
geolocation_source: 'browser',
|
|
222
227
|
};
|
|
223
228
|
|
|
224
229
|
expect(coordinates).toEqual({ lat: 19.4326, long: -99.1332 });
|
|
@@ -231,6 +236,7 @@ describe('geolocationService', () => {
|
|
|
231
236
|
'Geolocation Status': 'skipped_cache',
|
|
232
237
|
latitude: 19.4326,
|
|
233
238
|
longitude: -99.1332,
|
|
239
|
+
geolocation_source: 'browser',
|
|
234
240
|
});
|
|
235
241
|
});
|
|
236
242
|
|
|
@@ -286,6 +292,7 @@ describe('geolocationService', () => {
|
|
|
286
292
|
'Geolocation Accuracy': 15,
|
|
287
293
|
latitude: 19.4326,
|
|
288
294
|
longitude: -99.1332,
|
|
295
|
+
geolocation_source: 'browser',
|
|
289
296
|
},
|
|
290
297
|
);
|
|
291
298
|
|
|
@@ -331,6 +338,7 @@ describe('geolocationService', () => {
|
|
|
331
338
|
'Geolocation Accuracy': 8,
|
|
332
339
|
latitude: 20.9674,
|
|
333
340
|
longitude: -89.5926,
|
|
341
|
+
geolocation_source: 'browser',
|
|
334
342
|
},
|
|
335
343
|
);
|
|
336
344
|
|
|
@@ -14,6 +14,12 @@ interface GeolocationCache {
|
|
|
14
14
|
accuracy?: number;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Origin of the coordinates attached to geolocation events and profiles.
|
|
19
|
+
* - `browser` — Coordinates from the Geolocation API or a prior browser session cache.
|
|
20
|
+
*/
|
|
21
|
+
type GeolocationSource = 'browser';
|
|
22
|
+
|
|
17
23
|
interface GeolocationTrackingData {
|
|
18
24
|
status: GeolocationStatus;
|
|
19
25
|
updatedAt: string;
|
|
@@ -21,6 +27,7 @@ interface GeolocationTrackingData {
|
|
|
21
27
|
long?: number;
|
|
22
28
|
accuracy?: number;
|
|
23
29
|
errorCode?: number;
|
|
30
|
+
source?: GeolocationSource;
|
|
24
31
|
}
|
|
25
32
|
|
|
26
33
|
/**
|
|
@@ -35,6 +42,7 @@ interface GeolocationTrackingData {
|
|
|
35
42
|
type GeolocationStatus = 'granted' | 'denied' | 'error' | 'skipped_cache';
|
|
36
43
|
|
|
37
44
|
let currentCoordinates: Coordinates | null = null;
|
|
45
|
+
let currentSource: GeolocationSource | null = null;
|
|
38
46
|
|
|
39
47
|
const GEOLOCATION_PERMISSION_KEY = 'geolocationPermissionTimestamp';
|
|
40
48
|
const GEOLOCATION_CACHE_KEY = 'geolocationCache';
|
|
@@ -164,6 +172,7 @@ function restoreCoordinatesFromStorage(): void {
|
|
|
164
172
|
|
|
165
173
|
if (cache) {
|
|
166
174
|
currentCoordinates = { lat: cache.lat, long: cache.long };
|
|
175
|
+
currentSource = 'browser';
|
|
167
176
|
}
|
|
168
177
|
}
|
|
169
178
|
|
|
@@ -245,6 +254,10 @@ function buildGeolocationEventProperties(
|
|
|
245
254
|
properties.longitude = data.long;
|
|
246
255
|
}
|
|
247
256
|
|
|
257
|
+
if (data.source !== undefined) {
|
|
258
|
+
properties.geolocation_source = data.source;
|
|
259
|
+
}
|
|
260
|
+
|
|
248
261
|
return properties;
|
|
249
262
|
}
|
|
250
263
|
|
|
@@ -271,6 +284,10 @@ function buildGeolocationProfileProperties(
|
|
|
271
284
|
properties.longitude = data.long;
|
|
272
285
|
}
|
|
273
286
|
|
|
287
|
+
if (data.source !== undefined) {
|
|
288
|
+
properties.geolocation_source = data.source;
|
|
289
|
+
}
|
|
290
|
+
|
|
274
291
|
return properties;
|
|
275
292
|
}
|
|
276
293
|
|
|
@@ -311,6 +328,7 @@ function init(): Promise<Coordinates | null> {
|
|
|
311
328
|
lat: cache.lat,
|
|
312
329
|
long: cache.long,
|
|
313
330
|
accuracy: cache.accuracy,
|
|
331
|
+
source: 'browser',
|
|
314
332
|
});
|
|
315
333
|
resolve(currentCoordinates);
|
|
316
334
|
return;
|
|
@@ -330,6 +348,7 @@ function init(): Promise<Coordinates | null> {
|
|
|
330
348
|
(geolocation) => {
|
|
331
349
|
const { latitude: lat, longitude: long, accuracy } = geolocation.coords;
|
|
332
350
|
currentCoordinates = { lat, long };
|
|
351
|
+
currentSource = 'browser';
|
|
333
352
|
saveCoordinatesToStorage(currentCoordinates, updatedAt, accuracy);
|
|
334
353
|
updatePermissionTimestamp(now);
|
|
335
354
|
|
|
@@ -339,6 +358,7 @@ function init(): Promise<Coordinates | null> {
|
|
|
339
358
|
lat,
|
|
340
359
|
long,
|
|
341
360
|
accuracy,
|
|
361
|
+
source: 'browser',
|
|
342
362
|
});
|
|
343
363
|
resolve(currentCoordinates);
|
|
344
364
|
},
|
|
@@ -365,6 +385,14 @@ function getCoordinates(): Coordinates | null {
|
|
|
365
385
|
return currentCoordinates;
|
|
366
386
|
}
|
|
367
387
|
|
|
388
|
+
/**
|
|
389
|
+
* Returns the source of the currently cached coordinates, or null when unavailable.
|
|
390
|
+
* @returns {GeolocationSource | null} The geolocation source or null if no coordinates are cached.
|
|
391
|
+
*/
|
|
392
|
+
function getGeolocationSource(): GeolocationSource | null {
|
|
393
|
+
return currentSource;
|
|
394
|
+
}
|
|
395
|
+
|
|
368
396
|
/**
|
|
369
397
|
* Geolocation service for tracking user location with caching support.
|
|
370
398
|
* Handles permission requests, coordinate retrieval, and analytics sync to Mixpanel.
|
|
@@ -372,6 +400,7 @@ function getCoordinates(): Coordinates | null {
|
|
|
372
400
|
const geolocationService = {
|
|
373
401
|
init,
|
|
374
402
|
getCoordinates,
|
|
403
|
+
getGeolocationSource,
|
|
375
404
|
};
|
|
376
405
|
|
|
377
406
|
export default geolocationService;
|
package/src/track.ts
CHANGED
|
@@ -125,10 +125,14 @@ export async function trackEvent(
|
|
|
125
125
|
const fingerprint = await fingerprintService.getFingerprint(cacheOnly);
|
|
126
126
|
const defaultCoordinates: Record<string, string | number> = {};
|
|
127
127
|
const coordinates = geolocationService.getCoordinates();
|
|
128
|
+
const geolocationSource = geolocationService.getGeolocationSource();
|
|
128
129
|
if (coordinates) {
|
|
129
130
|
defaultCoordinates.latitude = coordinates.lat;
|
|
130
131
|
defaultCoordinates.longitude = coordinates.long;
|
|
131
132
|
}
|
|
133
|
+
if (geolocationSource) {
|
|
134
|
+
defaultCoordinates.geolocation_source = geolocationSource;
|
|
135
|
+
}
|
|
132
136
|
const defaultProperties = {
|
|
133
137
|
'User Fingerprint': fingerprint,
|
|
134
138
|
};
|