forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
53 lines (47 loc) · 1.65 KB
/
Copy pathindex.ts
File metadata and controls
53 lines (47 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { Plugin, SelfDescribingJson } from '@snowplow/tracker-core';
import { ApiPlugin, ApiMethods } from '@snowplow/browser-core';
import { Geolocation } from './contexts';
interface GeolocationMethods extends ApiMethods {
enableGeolocationContext: () => void;
}
const GeolocationPlugin = (enableAtLoad: boolean = false): Plugin & ApiPlugin<GeolocationMethods> => {
let geolocation: SelfDescribingJson<Geolocation>;
let geolocationContextAdded = false;
let navigatorAlias: Navigator = navigator;
if (enableAtLoad) {
enableGeolocationContext();
}
/**
* Attempts to create a context using the geolocation API and add it to commonContexts
*/
function enableGeolocationContext() {
if (!geolocationContextAdded && navigatorAlias.geolocation && navigatorAlias.geolocation.getCurrentPosition) {
geolocationContextAdded = true;
navigatorAlias.geolocation.getCurrentPosition(function (position) {
var coords = position.coords;
geolocation = {
schema: 'iglu:com.snowplowanalytics.snowplow/geolocation_context/jsonschema/1-1-0',
data: {
latitude: coords.latitude,
longitude: coords.longitude,
latitudeLongitudeAccuracy: coords.accuracy,
altitude: coords.altitude,
altitudeAccuracy: coords.altitudeAccuracy,
bearing: coords.heading,
speed: coords.speed,
timestamp: Math.round(position.timestamp),
},
};
});
}
}
return {
contexts: () => {
return geolocation ? [geolocation] : [];
},
apiMethods: {
enableGeolocationContext,
},
};
};
export { GeolocationPlugin };