|
1 | | -import { IAnalytics } from './common'; |
| 1 | +import { Utils } from '@nativescript/core'; |
| 2 | +import { Firebase } from '@nativescript/firebase-core'; |
| 3 | +import { ConsentStatus, ConsentType, EventParameter, IAnalytics } from './common'; |
| 4 | +export * from './common'; |
| 5 | + |
| 6 | +Firebase.analytics = () => { |
| 7 | + return new Analytics(); |
| 8 | +}; |
| 9 | + |
| 10 | +function serialize(data) { |
| 11 | + let store; |
| 12 | + |
| 13 | + switch (typeof data) { |
| 14 | + case 'string': |
| 15 | + case 'boolean': |
| 16 | + case 'number': { |
| 17 | + return data; |
| 18 | + } |
| 19 | + |
| 20 | + case 'object': { |
| 21 | + if (data === null) { |
| 22 | + return null; |
| 23 | + } |
| 24 | + |
| 25 | + if (data instanceof Date) { |
| 26 | + return data.toJSON(); |
| 27 | + } |
| 28 | + if (Array.isArray(data)) { |
| 29 | + store = new java.util.ArrayList(); |
| 30 | + data.forEach((item, index) => { |
| 31 | + const bundle = new android.os.Bundle(); |
| 32 | + const value = serialize(item); |
| 33 | + switch (typeof value) { |
| 34 | + case 'boolean': |
| 35 | + bundle.putBoolean(String(index), value); |
| 36 | + break; |
| 37 | + case 'number': |
| 38 | + bundle.putInt(String(index), value); |
| 39 | + break; |
| 40 | + case 'string': |
| 41 | + bundle.putString(String(index), value); |
| 42 | + break; |
| 43 | + case 'object': |
| 44 | + if (value instanceof android.os.Bundle) { |
| 45 | + bundle.putBundle(String(index), value); |
| 46 | + } else if (value instanceof java.util.ArrayList) { |
| 47 | + bundle.putParcelableArrayList(String(index), value); |
| 48 | + } else { |
| 49 | + bundle.putString(String(index), null); |
| 50 | + } |
| 51 | + |
| 52 | + break; |
| 53 | + } |
| 54 | + store.add(bundle); |
| 55 | + }); |
| 56 | + return store; |
| 57 | + } |
| 58 | + |
| 59 | + store = new android.os.Bundle(); |
| 60 | + Object.keys(data).forEach((key) => { |
| 61 | + const value = serialize(data[key]); |
| 62 | + switch (typeof value) { |
| 63 | + case 'boolean': |
| 64 | + store.putBoolean(key, value); |
| 65 | + break; |
| 66 | + case 'number': |
| 67 | + store.putInt(key, value); |
| 68 | + break; |
| 69 | + case 'string': |
| 70 | + store.putString(key, value); |
| 71 | + break; |
| 72 | + case 'object': |
| 73 | + if (value instanceof android.os.Bundle) { |
| 74 | + store.putBundle(key, value); |
| 75 | + } else if (value instanceof java.util.ArrayList) { |
| 76 | + store.putParcelableArrayList(key, value); |
| 77 | + } else { |
| 78 | + store.putString(key, null); |
| 79 | + } |
| 80 | + |
| 81 | + break; |
| 82 | + } |
| 83 | + }); |
| 84 | + return store; |
| 85 | + } |
| 86 | + |
| 87 | + default: |
| 88 | + return null; |
| 89 | + } |
| 90 | +} |
| 91 | + |
2 | 92 | export class Analytics implements IAnalytics { |
3 | | - constructor() {} |
4 | | - setUserProperty(value: string, name: string): void { |
5 | | - throw new Error('Method not implemented.'); |
| 93 | + #native: com.google.firebase.analytics.FirebaseAnalytics; |
| 94 | + |
| 95 | + constructor() { |
| 96 | + this.#native = com.google.firebase.analytics.FirebaseAnalytics.getInstance(Utils.android.getApplicationContext()); |
6 | 97 | } |
7 | | - setSessionTimeoutInterval(sessionTimeoutInterval: number): void { |
8 | | - throw new Error('Method not implemented.'); |
| 98 | + handleOpenURL(url: string): void {} |
| 99 | + |
| 100 | + handleUserActivity(userActivity: any): void {} |
| 101 | + |
| 102 | + get appInstanceId(): string { |
| 103 | + return this.#native.getAppInstanceId(); |
9 | 104 | } |
10 | | - setDefaultEventParameters(parameters: any): void { |
11 | | - throw new Error('Method not implemented.'); |
| 105 | + setSessionTimeoutInterval(sessionTimeoutInterval: number): void { |
| 106 | + this.#native.setSessionTimeoutDuration(sessionTimeoutInterval); |
12 | 107 | } |
13 | | - setConsent(consentSettings: any): void { |
14 | | - throw new Error('Method not implemented.'); |
| 108 | + setUserProperty(key: string, value: string): void { |
| 109 | + this.#native.setUserProperty(key, value); |
15 | 110 | } |
16 | | - handleOpenURL(url: string): void { |
17 | | - throw new Error('Method not implemented.'); |
| 111 | + setAnalyticsCollectionEnabled(analyticsCollectionEnabled: boolean): void { |
| 112 | + this.#native.setAnalyticsCollectionEnabled(analyticsCollectionEnabled); |
18 | 113 | } |
19 | | - handleUserActivity(userActivity: any): void { |
20 | | - throw new Error('Method not implemented.'); |
| 114 | + setUserId(userId: string): void { |
| 115 | + this.#native.setUserId(userId); |
21 | 116 | } |
22 | | - get appInstanceId(): string { |
23 | | - return ''; |
| 117 | + logEvent(name: string, parameters: EventParameter): void { |
| 118 | + this.#native.logEvent(name, serialize(parameters)); |
24 | 119 | } |
25 | | - setAnalyticsCollectionEnabled(analyticsCollectionEnabled: boolean): void { |
26 | | - throw new Error('Method not implemented.'); |
| 120 | + resetAnalyticsData(): void { |
| 121 | + this.#native.resetAnalyticsData(); |
27 | 122 | } |
28 | | - logEvent(name: string): void { |
29 | | - throw new Error('Method not implemented.'); |
| 123 | + |
| 124 | + setDefaultEventParameters(parameters: EventParameter): void { |
| 125 | + this.#native.setDefaultEventParameters(serialize(parameters)); |
30 | 126 | } |
31 | | - setUserId(userId: string): void { |
32 | | - throw new Error('Method not implemented.'); |
| 127 | + |
| 128 | + setConsent(consentSettings: Map<ConsentType, ConsentStatus>): void { |
| 129 | + const nativeMap = new java.util.HashMap(); |
| 130 | + consentSettings.forEach((value, key) => { |
| 131 | + let nativeKey; |
| 132 | + let nativeValue; |
| 133 | + switch (key) { |
| 134 | + case ConsentType.Ad_Storage: |
| 135 | + nativeKey = FIRConsentTypeAdStorage; |
| 136 | + break; |
| 137 | + case ConsentType.Analytics_Storage: |
| 138 | + nativeKey = FIRConsentTypeAnalyticsStorage; |
| 139 | + break; |
| 140 | + } |
| 141 | + |
| 142 | + switch (value) { |
| 143 | + case ConsentStatus.Denied: |
| 144 | + nativeValue = FIRConsentStatusDenied; |
| 145 | + break; |
| 146 | + case ConsentStatus.Granted: |
| 147 | + nativeValue = FIRConsentStatusGranted; |
| 148 | + break; |
| 149 | + } |
| 150 | + if (nativeKey && nativeValue) { |
| 151 | + nativeMap.put(nativeKey, nativeValue); |
| 152 | + } |
| 153 | + }); |
| 154 | + this.#native.setConsent(nativeMap); |
33 | 155 | } |
34 | | - resetAnalyticsData(): void {} |
35 | 156 | } |
0 commit comments