-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathanalytics.js
More file actions
109 lines (91 loc) · 3 KB
/
Copy pathanalytics.js
File metadata and controls
109 lines (91 loc) · 3 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { Platform, Dimensions } from "react-native";
import { Constants } from "expo";
import { ScreenHit, PageHit, Event, Serializable } from "./hits";
import HitValidator from "./validation";
const { width, height } = Dimensions.get("window");
let defaultOptions = { debug: false };
export default class Analytics {
customDimensions = [];
constructor(propertyId, additionalParameters = {}, options = defaultOptions) {
this.propertyId = propertyId;
this.options = options;
this.clientId = Constants.deviceId;
this.hitValidator = new HitValidator({ debug: this.options.debug });
this.promiseGetWebViewUserAgentAsync = Constants.getWebViewUserAgentAsync().then(
userAgent => {
this.userAgent = userAgent;
this.parameters = {
an: Constants.manifest.name,
aid: Constants.manifest.slug,
av: Constants.manifest.version,
sr: `${width}x${height}`,
...additionalParameters
};
if (this.options.debug) {
console.log(`[expo-analytics] UserAgent=${userAgent}`);
console.log(
`[expo-analytics] Additional parameters=`,
this.parameters
);
}
}
);
}
hit(hit) {
// send only after the user agent is saved
return this.promiseGetWebViewUserAgentAsync.then(() => this.send(hit));
}
event(event) {
// send only after the user agent is saved
return this.promiseGetWebViewUserAgentAsync.then(() => this.send(event));
}
addCustomDimension(index, value) {
this.customDimensions[index] = value;
}
removeCustomDimension(index) {
delete this.customDimensions[index];
}
send(hit) {
/* format: https://www.google-analytics.com/collect? +
* &tid= GA property ID (required)
* &v= GA protocol version (always 1) (required)
* &t= hit type (pageview / screenview)
* &dp= page name (if hit type is pageview)
* &cd= screen name (if hit type is screenview)
* &cid= anonymous client ID (optional if uid is given)
* &uid= user id (optional if cid is given)
* &ua= user agent override
* &an= app name (required for any of the other app parameters to work)
* &aid= app id
* &av= app version
* &sr= screen resolution
* &cd{n}= custom dimensions
* &z= cache buster (prevent browsers from caching GET requests -- should always be last)
*/
const customDimensions = this.customDimensions
.map((value, index) => `cd${index}=${value}`)
.join("&");
const params = new Serializable(this.parameters).toQueryString();
const url = `https://www.google-analytics.com/collect?tid=${
this.propertyId
}&v=1&cid=${
this.clientId
}&${hit.toQueryString()}&${params}&${customDimensions}&z=${Math.round(
Math.random() * 1e8
)}`;
let options = {
method: "get",
headers: {
"User-Agent": this.userAgent
}
};
return this.hitValidator.validate({ url }).then(valid => {
if (valid) {
if (this.options.debug) {
console.log(`[expo-analytics] Sending GET request to ${url}`);
}
return fetch(url, options);
}
});
}
}