|
23 | 23 | import { makeCloudFunction, CloudFunction, Event } from '../cloud-functions'; |
24 | 24 | import * as _ from 'lodash'; |
25 | 25 |
|
| 26 | +/** @internal */ |
| 27 | +export const provider = 'google.firebase.analytics'; |
| 28 | + |
26 | 29 | /** Handle events sent to Firebase Analytics. */ |
27 | 30 | export function event(analyticsEventType: string) { |
28 | 31 | return new AnalyticsEventBuilder( |
@@ -50,8 +53,102 @@ export class AnalyticsEventBuilder { |
50 | 53 | } |
51 | 54 | } |
52 | 55 |
|
53 | | -/** @internal */ |
54 | | -export const provider = 'google.firebase.analytics'; |
| 56 | +/** A collection of information about a Firebase Analytics event that was logged for a specific user. */ |
| 57 | +export class AnalyticsEvent { |
| 58 | + /** The date on which the event.was logged. |
| 59 | + * (YYYYMMDD format in the registered timezone of your app.) |
| 60 | + */ |
| 61 | + reportingDate: string; |
| 62 | + |
| 63 | + /** The name of the event. */ |
| 64 | + name: string; |
| 65 | + |
| 66 | + /** A repeated record of the parameters associated with the event. |
| 67 | + * Note: this value is cast to its most appropriate type, which due to the nature of JavaScript's number |
| 68 | + * handling might entail a loss of precision in case of very large integers. |
| 69 | + */ |
| 70 | + params: { [key: string]: any }; |
| 71 | + |
| 72 | + /** UTC client time when the event happened. */ |
| 73 | + logTime: string; |
| 74 | + |
| 75 | + /** UTC client time when the previous event happened. */ |
| 76 | + previousLogTime?: string; |
| 77 | + |
| 78 | + /** Value param in USD. */ |
| 79 | + valueInUSD?: number; |
| 80 | + |
| 81 | + /** User related dimensions. */ |
| 82 | + user?: UserDimensions; |
| 83 | + |
| 84 | + /** @internal */ |
| 85 | + constructor(wireFormat: any) { |
| 86 | + this.params = {}; // In case of absent field, show empty (not absent) map. |
| 87 | + if (wireFormat.eventDim && wireFormat.eventDim.length > 0) { |
| 88 | + // If there's an eventDim, there'll always be exactly one. |
| 89 | + let eventDim = wireFormat.eventDim[0]; |
| 90 | + copyField(eventDim, this, 'name'); |
| 91 | + copyField(eventDim, this, 'params', p => _.mapValues(p, unwrapValue)); |
| 92 | + copyFieldTo(eventDim, this, 'valueInUsd', 'valueInUSD'); |
| 93 | + copyFieldTo(eventDim, this, 'date', 'reportingDate'); |
| 94 | + copyTimestampToString(eventDim, this, 'timestampMicros', 'logTime'); |
| 95 | + copyTimestampToString(eventDim, this, 'previousTimestampMicros', 'previousLogTime'); |
| 96 | + } |
| 97 | + copyFieldTo(wireFormat, this, 'userDim', 'user', dim => new UserDimensions(dim)); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +/** A collection of information about the user who triggered these events. */ |
| 102 | +export class UserDimensions { |
| 103 | + /* tslint:disable:max-line-length */ |
| 104 | + /** The user ID set via the setUserId API. |
| 105 | + * https://firebase.google.com/docs/reference/android/com/google/firebase/analytics/FirebaseAnalytics.html#setUserId(java.lang.String) |
| 106 | + * https://firebase.google.com/docs/reference/ios/firebaseanalytics/api/reference/Classes/FIRAnalytics#/c:objc(cs)FIRAnalytics(cm)setUserID |
| 107 | + */ |
| 108 | + userId?: string; |
| 109 | + /* tslint:enable:max-line-length */ |
| 110 | + |
| 111 | + /** The time (in UTC) at which the user first opened the app. */ |
| 112 | + firstOpenTime?: string; |
| 113 | + |
| 114 | + /** A repeated record of user properties set with the setUserProperty API. |
| 115 | + * https://firebase.google.com/docs/analytics/android/properties |
| 116 | + */ |
| 117 | + userProperties: { [key: string]: UserPropertyValue }; |
| 118 | + |
| 119 | + /** Device information. */ |
| 120 | + deviceInfo: DeviceInfo; |
| 121 | + |
| 122 | + /** User's geographic information. */ |
| 123 | + geoInfo: GeoInfo; |
| 124 | + |
| 125 | + /** App information. */ |
| 126 | + appInfo?: AppInfo; |
| 127 | + |
| 128 | + /** Information about the marketing campaign which acquired the user. */ |
| 129 | + trafficSource?: TrafficSource; |
| 130 | + |
| 131 | + /** Information regarding the bundle in which these events were uploaded. */ |
| 132 | + bundleInfo: ExportBundleInfo; |
| 133 | + |
| 134 | + /** Lifetime Value revenue of this user, in USD. */ |
| 135 | + ltvInUSD?: number; |
| 136 | + |
| 137 | + /** @internal */ |
| 138 | + constructor(wireFormat: any) { |
| 139 | + // These are interfaces or primitives, no transformation needed. |
| 140 | + copyFields(wireFormat, this, ['userId', 'deviceInfo', 'geoInfo', 'appInfo', 'trafficSource']); |
| 141 | + |
| 142 | + // The following fields do need transformations of some sort. |
| 143 | + copyTimestampToString(wireFormat, this, 'firstOpenTimestampMicros', 'firstOpenTime'); |
| 144 | + this.userProperties = {}; // With no entries in the wire format, present an empty (as opposed to absent) map. |
| 145 | + copyField(wireFormat, this, 'userProperties', r => _.mapValues(r, p => new UserPropertyValue(p))); |
| 146 | + copyField(wireFormat, this, 'bundleInfo', r => new ExportBundleInfo(r)); |
| 147 | + if (wireFormat.ltvInfo && wireFormat.ltvInfo.currency === 'USD') { |
| 148 | + this.ltvInUSD = wireFormat.ltvInfo.revenue; |
| 149 | + } |
| 150 | + } |
| 151 | +} |
55 | 152 |
|
56 | 153 | /** Predefined (eg: LTV) or custom properties (eg: birthday) stored on client side and associated with |
57 | 154 | * subsequent HitBundles. |
@@ -183,103 +280,6 @@ export class ExportBundleInfo { |
183 | 280 | } |
184 | 281 | } |
185 | 282 |
|
186 | | -/** A collection of information about the user who triggered these events. */ |
187 | | -export class UserDimensions { |
188 | | - /* tslint:disable:max-line-length */ |
189 | | - /** The user ID set via the setUserId API. |
190 | | - * https://firebase.google.com/docs/reference/android/com/google/firebase/analytics/FirebaseAnalytics.html#setUserId(java.lang.String) |
191 | | - * https://firebase.google.com/docs/reference/ios/firebaseanalytics/api/reference/Classes/FIRAnalytics#/c:objc(cs)FIRAnalytics(cm)setUserID |
192 | | - */ |
193 | | - userId?: string; |
194 | | - /* tslint:enable:max-line-length */ |
195 | | - |
196 | | - /** The time (in UTC) at which the user first opened the app. */ |
197 | | - firstOpenTime?: string; |
198 | | - |
199 | | - /** A repeated record of user properties set with the setUserProperty API. |
200 | | - * https://firebase.google.com/docs/analytics/android/properties |
201 | | - */ |
202 | | - userProperties: { [key: string]: UserPropertyValue }; |
203 | | - |
204 | | - /** Device information. */ |
205 | | - deviceInfo: DeviceInfo; |
206 | | - |
207 | | - /** User's geographic information. */ |
208 | | - geoInfo: GeoInfo; |
209 | | - |
210 | | - /** App information. */ |
211 | | - appInfo?: AppInfo; |
212 | | - |
213 | | - /** Information about the marketing campaign which acquired the user. */ |
214 | | - trafficSource?: TrafficSource; |
215 | | - |
216 | | - /** Information regarding the bundle in which these events were uploaded. */ |
217 | | - bundleInfo: ExportBundleInfo; |
218 | | - |
219 | | - /** Lifetime Value revenue of this user, in USD. */ |
220 | | - ltvInUSD?: number; |
221 | | - |
222 | | - /** @internal */ |
223 | | - constructor(wireFormat: any) { |
224 | | - // These are interfaces or primitives, no transformation needed. |
225 | | - copyFields(wireFormat, this, ['userId', 'deviceInfo', 'geoInfo', 'appInfo', 'trafficSource']); |
226 | | - |
227 | | - // The following fields do need transformations of some sort. |
228 | | - copyTimestampToString(wireFormat, this, 'firstOpenTimestampMicros', 'firstOpenTime'); |
229 | | - this.userProperties = {}; // With no entries in the wire format, present an empty (as opposed to absent) map. |
230 | | - copyField(wireFormat, this, 'userProperties', r => _.mapValues(r, p => new UserPropertyValue(p))); |
231 | | - copyField(wireFormat, this, 'bundleInfo', r => new ExportBundleInfo(r)); |
232 | | - if (wireFormat.ltvInfo && wireFormat.ltvInfo.currency === 'USD') { |
233 | | - this.ltvInUSD = wireFormat.ltvInfo.revenue; |
234 | | - } |
235 | | - } |
236 | | -} |
237 | | - |
238 | | -/** A collection of information about a Firebase Analytics event that was logged for a specific user. */ |
239 | | -export class AnalyticsEvent { |
240 | | - /** The date on which the event.was logged. |
241 | | - * (YYYYMMDD format in the registered timezone of your app.) |
242 | | - */ |
243 | | - reportingDate: string; |
244 | | - |
245 | | - /** The name of the event. */ |
246 | | - name: string; |
247 | | - |
248 | | - /** A repeated record of the parameters associated with the event. |
249 | | - * Note: this value is cast to its most appropriate type, which due to the nature of JavaScript's number |
250 | | - * handling might entail a loss of precision in case of very large integers. |
251 | | - */ |
252 | | - params: { [key: string]: any }; |
253 | | - |
254 | | - /** UTC client time when the event happened. */ |
255 | | - logTime: string; |
256 | | - |
257 | | - /** UTC client time when the previous event happened. */ |
258 | | - previousLogTime?: string; |
259 | | - |
260 | | - /** Value param in USD. */ |
261 | | - valueInUSD?: number; |
262 | | - |
263 | | - /** User related dimensions. */ |
264 | | - user?: UserDimensions; |
265 | | - |
266 | | - /** @internal */ |
267 | | - constructor(wireFormat: any) { |
268 | | - this.params = {}; // In case of absent field, show empty (not absent) map. |
269 | | - if (wireFormat.eventDim && wireFormat.eventDim.length > 0) { |
270 | | - // If there's an eventDim, there'll always be exactly one. |
271 | | - let eventDim = wireFormat.eventDim[0]; |
272 | | - copyField(eventDim, this, 'name'); |
273 | | - copyField(eventDim, this, 'params', p => _.mapValues(p, unwrapValue)); |
274 | | - copyFieldTo(eventDim, this, 'valueInUsd', 'valueInUSD'); |
275 | | - copyFieldTo(eventDim, this, 'date', 'reportingDate'); |
276 | | - copyTimestampToString(eventDim, this, 'timestampMicros', 'logTime'); |
277 | | - copyTimestampToString(eventDim, this, 'previousTimestampMicros', 'previousLogTime'); |
278 | | - } |
279 | | - copyFieldTo(wireFormat, this, 'userDim', 'user', dim => new UserDimensions(dim)); |
280 | | - } |
281 | | -} |
282 | | - |
283 | 283 | function copyFieldTo<T, K extends keyof T>( |
284 | 284 | from: any, to: T, fromField: string, toField: K, transform = _.identity): void { |
285 | 285 | if (from[fromField] !== undefined) { |
|
0 commit comments