1 | import { Platform, Dimensions } from 'react-native';
|
2 | import Constants from 'expo-constants';
|
3 |
|
4 | import { ScreenHit, PageHit, Event, Serializable } from './hits';
|
5 |
|
6 | const { width, height } = Dimensions.get('window');
|
7 |
|
8 | let defaultOptions = { debug: false };
|
9 |
|
10 | let webViewUserAgent = null;
|
11 | const getWebViewUserAgent = async (options) => {
|
12 | return new Promise((resolve) => {
|
13 | if (options.userAgent) {
|
14 | webViewUserAgent = options.userAgent;
|
15 | return resolve(options.userAgent);
|
16 | }
|
17 | if (webViewUserAgent) return resolve(webViewUserAgent);
|
18 | Constants.getWebViewUserAgentAsync()
|
19 | .then(userAgent => {
|
20 | webViewUserAgent = userAgent;
|
21 | resolve(userAgent);
|
22 | })
|
23 | .catch(() => resolve('unknown user agent'))
|
24 | });
|
25 | }
|
26 |
|
27 | export default class Analytics {
|
28 | customDimensions = []
|
29 | customMetrics = []
|
30 |
|
31 | constructor(propertyId, additionalParameters = {}, options = defaultOptions){
|
32 | this.propertyId = propertyId;
|
33 | this.options = options;
|
34 | this.clientId = Constants.deviceId;
|
35 | this.parameters = {
|
36 | an: Constants.manifest.name,
|
37 | aid: Constants.manifest.slug,
|
38 | av: Constants.manifest.version,
|
39 | sr: `${width}x${height}`,
|
40 | ...additionalParameters
|
41 | };
|
42 |
|
43 | this.promiseGetWebViewUserAgentAsync = getWebViewUserAgent(options)
|
44 | .then(userAgent => {
|
45 | this.userAgent = userAgent;
|
46 | if(this.options.debug){
|
47 | console.log(`[expo-analytics] UserAgent=${userAgent}`);
|
48 | console.log(`[expo-analytics] Additional parameters=`, this.parameters);
|
49 | }
|
50 | });
|
51 | }
|
52 |
|
53 | hit(hit){
|
54 |
|
55 | return this.promiseGetWebViewUserAgentAsync
|
56 | .then(() => this.send(hit));
|
57 | }
|
58 |
|
59 | event(event){
|
60 |
|
61 | return this.promiseGetWebViewUserAgentAsync
|
62 | .then(() => this.send(event));
|
63 | }
|
64 |
|
65 | addParameter(name, value){
|
66 | this.parameters[name] = value;
|
67 | }
|
68 |
|
69 | addCustomDimension(index, value){
|
70 | this.customDimensions[index] = value;
|
71 | }
|
72 |
|
73 | removeCustomDimension(index){
|
74 | delete this.customDimensions[index];
|
75 | }
|
76 |
|
77 | addCustomMetric(index, value) {
|
78 | this.customMetrics[index] = value;
|
79 | }
|
80 |
|
81 | removeCustomMetric(index) {
|
82 | delete this.customMetrics[index];
|
83 | }
|
84 |
|
85 | send(hit) {
|
86 | |
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | const customDimensions = this.customDimensions.map((value, index) => `cd${index}=${value}`).join('&');
|
119 | const customMetrics = this.customMetrics.map((value, index) => `cm${index}=${value}`).join('&');
|
120 |
|
121 | const params = new Serializable(this.parameters).toQueryString();
|
122 |
|
123 | const url = `https://www.google-analytics.com/collect?tid=${this.propertyId}&v=1&cid=${this.clientId}&${hit.toQueryString()}&${params}&${customDimensions}&${customMetrics}&z=${Math.round(Math.random() * 1e8)}`;
|
124 |
|
125 | let options = {
|
126 | method: 'get',
|
127 | headers: {
|
128 | 'User-Agent': this.userAgent
|
129 | }
|
130 | }
|
131 |
|
132 | if(this.options.debug){
|
133 | console.log(`[expo-analytics] Sending GET request to ${url}`);
|
134 | }
|
135 |
|
136 | return fetch(url, options);
|
137 | }
|
138 |
|
139 | }
|