UNPKG

6.92 kBMarkdownView Raw
1[![npm version](https://badge.fury.io/js/expo-analytics.png)](https://badge.fury.io/js/expo-analytics)
2
3Expo Analytics
4=========
5
6Google Analytics integration for use with React Native apps built on Expo. Most of the other Google Analytics libraries I've found require linking, which is not supported with Expo. This library does not require linking.
7
8Please create issues for any bugs you find or features you would like added.
9
10## Installation
11
12```
13npm install expo-analytics --save
14```
15
16## Support for web + app projects
17Selecting this option when creating a google analytics property the tracking ID it is not prefixed with `'UA-'` but with `G-` resulting in views not showing up. For now use the regular GA property pending resolving issue [#48](https://github.com/ryanvanderpol/expo-analytics/issues/48)
18
19## Breaking Changes in Expo SDK 33
20
21It seems that Expo introduced some breaking changes in SDK 33, so if you are using a version of Expo below 33 please pin your `package.json` to version `1.0.8` of this package. `expo-analytics` `1.0.9+` is only compatable with Expo SDK 33+.
22
23## Usage
24
25Your React Native app's screen resolution, app name, app ID, app version and multiple other parameters will be automatically resolved and sent with each hit or event.
26
27##### Hits
28
29Sending page hits or screen hits is done by constructing a new `PageHit` or `ScreenHit` instance and passing it to the `hit` function of an `Analytics` instance.
30
31```
32import { Analytics, PageHit } from 'expo-analytics';
33
34const analytics = new Analytics('UA-XXXXXX-Y');
35analytics.hit(new PageHit('Home'))
36 .then(() => console.log("success"))
37 .catch(e => console.log(e.message));
38```
39
40##### Events
41
42You can also send custom events by constructing a new `Event` instance and passing it to the `event` function. Events have four parameters.
43
44* Event Category
45* Event Action
46* Event Label (optional, but recommended)
47* Event Value (optional, integer)
48
49These parameters are passed to the `Event` constructor in that order.
50
51```
52import { Analytics, Event } from 'expo-analytics';
53
54const analytics = new Analytics('UA-XXXXXX-Y');
55analytics.event(new Event('Video', 'Play', 'The Big Lebowski', 123))
56 .then(() => console.log("success"))
57 .catch(e => console.log(e.message));
58```
59
60[Learn more](https://support.google.com/analytics/answer/1033068?hl=en) about custom events.
61
62##### Custom Dimensions
63
64[Custom Dimensions](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#cd_) are also supported. Any custom dimensions set will be sent with each request.
65
66```
67import { Analytics, Event } from 'expo-analytics';
68
69const analytics = new Analytics('UA-XXXXXX-Y');
70analytics.addCustomDimension(1, 'TrialAccount');
71analytics.addCustomDimension(2, 'Comedy');
72analytics.event(new Event('Video', 'Play', 'The Big Lebowski', 123))
73 .then(() => console.log("success"))
74 .catch(e => console.log(e.message));
75```
76
77You can remove custom dimensions as needed.
78
79```
80analytics.removeCustomDimension(1);
81```
82
83##### Custom Metrics
84
85[Custom Metrics](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#cm_) work the same way with just a slightly different call.
86
87```
88import { Analytics, Event } from 'expo-analytics';
89
90const analytics = new Analytics('UA-XXXXXX-Y');
91analytics.addCustomMetric(1, 15);
92analytics.removeCustomMetric(1);
93```
94
95##### Additional Parameters
96
97You can also optionally include any additional [supported parameters](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters) you would like.
98
99```
100import { Analytics } from 'expo-analytics';
101
102// pass in the user ID (uid), referrer (dr) and campaign name (cn)
103const analytics = new Analytics('UA-XXXXXX-Y', { uid: '999', dr: 'github.com', cn: 'get_more_views' });
104```
105
106##### Ecommerce tracking
107###### Transaction hit type
108You can also send purchase by constructing a new `Transaction` instance and passing it to the `transaction` function. Transaction have five parameters.
109
110* id (Required, string)
111* affiliation (Optional, string)
112* revenue (Optional, currency, but recommended)
113* shipping (Optional, currency)
114* tax (Optional, currency)
115
116These parameters are passed to the `Transaction` constructor in that order.
117
118```
119import { Analytics, Transaction } from 'expo-analytics';
120
121const analytics = new Analytics('UA-XXXXXX-Y');
122
123 analytics.hit(new Transaction('1235', 'Store', 38.43, 1.29, 5))
124 .then(() => console.log("success"))
125 .catch(e => console.log(e.message));
126```
127
128###### Item hit type
129You can also send along the purchase the products that were purchased in the transaction, constructing a new `AddItem` instance and passing it to the `AddItem` function. 'AddItem' have six parameters.
130
131* id (The transaction id, Required, string)
132* name (Required, string)
133* price (Optional, currency, but recommended)
134* quantity (Optional, integer)
135* sku (Optional, string, but recommended)
136* category (Optional, string, but recommended)
137
138These parameters are passed to the `AddItem` constructor in that order.
139
140```
141import { Analytics, AddItem } from 'expo-analytics';
142
143const analytics = new Analytics('UA-XXXXXX-Y');
144
145 analytics.hit(new AddItem('1235', 'T-SHIRT', 11.99, 1, 'DD44', 'Clothes'))
146 .then(() => console.log("success"))
147 .catch(e => console.log(e.message));
148```
149
150## Debugging
151
152The Google Analytics API is a bit particular. If you're not seeing Real Time hits in your Analytics console you can turn on debug mode for this package and the exact URL request being sent will be printed to the `console`.
153
154```
155import { Analytics, PageHit } from 'expo-analytics';
156
157const analytics = new Analytics('UA-XXXXXX-Y', null, { debug: true });
158analytics.hit(new PageHit('IsItWorking'))
159 .then(() => console.log("success"))
160 .catch(e => console.log(e.message));
161```
162
163## More Options
164
165You might want to use your own static userAgent http header instead of the default WebView header.
166```
167const analytics = new Analytics('UA-XXXXXX-Y', null, { userAgent: 'Custom UserAgent' });
168```
169
170
171## Release History
172
173* 1.0.14 Fixing a possible race condition. Thanks, @giautm!
174
175* 1.0.13 User agent caching (thanks, @musemind!) and screenTitle on PageHits (thanks, @YassineElbouchaibi)
176
177* 1.0.11 Support for e-commerce tracking. Thanks, @lucianfialhobp.
178
179* 1.0.10 Support for custom metrics.
180
181* 1.0.9 Support for Expo 0.33. Thanks, @rossb89.
182
183* 1.0.8 Adding TypeScript definitions.
184
185* 1.0.7 Promisification. Thanks, @dylancompanjen!
186
187* 1.0.6 Fix for `ScreenHit` parameter name (thanks @davisml!). Support for custom dimensions.
188
189* 1.0.5 Fixing issue with duplicate dependencies causing installation problems.
190
191* 1.0.4 Automatically pull screen resolution from React Native dimensions. Resolve app name, app ID, app version from Expo manifest. App name is now required. Added debug option.
192
193* 1.0.3 Added support for app ID and version
194
195* 1.0.2 Added support for custom events
196
197* 1.0.1 Initial release
198
\No newline at end of file