Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit bc9ee1b

Browse files
Analytics Opt-Out #862
1 parent aec02e1 commit bc9ee1b

File tree

9 files changed

+90
-26
lines changed

9 files changed

+90
-26
lines changed

demo/app/main-page.xml

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
<TabViewItem.view>
8282
<ScrollView>
8383
<GridLayout columns="*, *"
84-
rows="auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto"
84+
rows="auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto"
8585
horizontalAlignment="stretch"
8686
class="tab-content">
8787

@@ -169,45 +169,48 @@
169169

170170
<Label row="30" colSpan="2" text="Analytics" class="subtitle"/>
171171

172-
<Button row="31" col="0" text="log event" tap="{{ doLogAnalyticsEvent }}" class="button button-analytics"/>
173-
<Button row="31" col="1" text="set user property" tap="{{ doSetAnalyticsUserProperty }}" class="button button-analytics"/>
172+
<Button row="31" col="0" text="collection on" tap="{{ doEnableAnalytics }}" class="button button-analytics"/>
173+
<Button row="31" col="1" text="collection off" tap="{{ doDisableAnalytics }}" class="button button-analytics"/>
174174

175-
<Button row="32" col="0" text="set Screen A" tap="{{ doSetScreenNameA }}" class="button button-analytics"/>
176-
<Button row="32" col="1" text="set Screen B" tap="{{ doSetScreenNameB }}" class="button button-analytics"/>
175+
<Button row="32" col="0" text="log event" tap="{{ doLogAnalyticsEvent }}" class="button button-analytics"/>
176+
<Button row="32" col="1" text="set user property" tap="{{ doSetAnalyticsUserProperty }}" class="button button-analytics"/>
177177

178-
<Label row="33" colSpan="2" text="AdMob" class="subtitle"/>
178+
<Button row="33" col="0" text="set Screen A" tap="{{ doSetScreenNameA }}" class="button button-analytics"/>
179+
<Button row="33" col="1" text="set Screen B" tap="{{ doSetScreenNameB }}" class="button button-analytics"/>
179180

180-
<Button row="34" col="0" text="show banner" tap="{{ doShowAdMobBanner }}" class="button button-admob"/>
181-
<Button row="34" col="1" text="show interstitial" tap="{{ doShowAdMobInterstitial }}" class="button button-admob"/>
181+
<Label row="34" colSpan="2" text="AdMob" class="subtitle"/>
182182

183-
<Button row="35" colSpan="2" text="hide banner" tap="{{ doHideAdMobBanner }}" class="button button-admob"/>
183+
<Button row="35" col="0" text="show banner" tap="{{ doShowAdMobBanner }}" class="button button-admob"/>
184+
<Button row="35" col="1" text="show interstitial" tap="{{ doShowAdMobInterstitial }}" class="button button-admob"/>
184185

185-
<Label row="36" colSpan="2" text="Firebase Cloud Messaging" class="subtitle"/>
186+
<Button row="36" colSpan="2" text="hide banner" tap="{{ doHideAdMobBanner }}" class="button button-admob"/>
186187

187-
<Button row="37" col="0" text="add push handlers" tap="{{ messaging.doRegisterPushHandlers }}" class="button button-messaging"/>
188-
<Button row="37" col="1" text="get current token" tap="{{ messaging.doGetCurrentPushToken }}" class="button button-messaging"/>
188+
<Label row="37" colSpan="2" text="Firebase Cloud Messaging" class="subtitle"/>
189189

190-
<Button row="38" col="0" text="topic subscribe" tap="{{ messaging.doSubscribeToTopic }}" class="button button-messaging"/>
191-
<Button row="38" col="1" text="topic unsubscribe" tap="{{ messaging.doUnsubscribeFromTopic }}" class="button button-messaging"/>
190+
<Button row="38" col="0" text="add push handlers" tap="{{ messaging.doRegisterPushHandlers }}" class="button button-messaging"/>
191+
<Button row="38" col="1" text="get current token" tap="{{ messaging.doGetCurrentPushToken }}" class="button button-messaging"/>
192+
193+
<Button row="39" col="0" text="topic subscribe" tap="{{ messaging.doSubscribeToTopic }}" class="button button-messaging"/>
194+
<Button row="39" col="1" text="topic unsubscribe" tap="{{ messaging.doUnsubscribeFromTopic }}" class="button button-messaging"/>
192195
<iOS>
193-
<Button row="39" colSpan="2" text="register interactive push" tap="{{ messaging.doRegisterForInteractivePush }}" class="button button-messaging"/>
196+
<Button row="40" colSpan="2" text="register interactive push" tap="{{ messaging.doRegisterForInteractivePush }}" class="button button-messaging"/>
194197
</iOS>
195-
<Button row="40" colSpan="2" text="are notifications enabled" tap="{{ messaging.doGetAreNotificationsEnabled }}" class="button button-messaging"/>
198+
<Button row="41" colSpan="2" text="are notifications enabled" tap="{{ messaging.doGetAreNotificationsEnabled }}" class="button button-messaging"/>
196199
<!-- <Button row="39" col="1" text="topic unsubscribe" tap="{{ doUnsubscribeFromTopic }}" class="button button-messaging"/> -->
197200

198-
<Label row="41" colSpan="2" text="Firebase Crash Reporing / Crashlytics" class="subtitle"/>
201+
<Label row="42" colSpan="2" text="Firebase Crash Reporing / Crashlytics" class="subtitle"/>
199202

200-
<Button row="42" col="0" text="log message" tap="{{ doLogMessage }}" class="button button-crash"/>
203+
<Button row="43" col="0" text="log message" tap="{{ doLogMessage }}" class="button button-crash"/>
201204
<iOS>
202-
<Button row="42" col="1" text="force crash :)" tap="{{ doForceCrashIOS }}" class="button button-crash"/>
205+
<Button row="43" col="1" text="force crash :)" tap="{{ doForceCrashIOS }}" class="button button-crash"/>
203206
</iOS>
204207
<Android>
205-
<Button row="42" col="1" text="force crash :)" tap="{{ doForceCrashAndroid }}" class="button button-crash"/>
208+
<Button row="43" col="1" text="force crash :)" tap="{{ doForceCrashAndroid }}" class="button button-crash"/>
206209
</Android>
207210

208-
<Label row="43" colSpan="2" text="Firebase Invites" class="subtitle"/>
209-
<Button row="44" col="0" text="send invitation" tap="{{ sendInvitation }}" class="button button-invites"/>
210-
<Button row="44" col="1" text="get invitation" tap="{{ getInvitation }}" class="button button-invites"/>
211+
<Label row="44" colSpan="2" text="Firebase Invites" class="subtitle"/>
212+
<Button row="45" col="0" text="send invitation" tap="{{ sendInvitation }}" class="button button-invites"/>
213+
<Button row="45" col="1" text="get invitation" tap="{{ getInvitation }}" class="button button-invites"/>
211214
</GridLayout>
212215

213216
</ScrollView>

demo/app/main-view-model.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ export class HelloWorldModel extends Observable {
378378
firebase.init({
379379
// storageBucket: 'gs://n-plugin-test.appspot.com',
380380
persist: true, // optional, default false
381+
analyticsCollectionEnabled: true,
381382
onAuthStateChanged: data => { // optional
382383
console.log((data.loggedIn ? "Logged in to firebase" : "Logged out from firebase") + " (init's onAuthStateChanged callback)");
383384
if (data.loggedIn) {
@@ -427,6 +428,24 @@ export class HelloWorldModel extends Observable {
427428
);
428429
}
429430

431+
public doEnableAnalytics(): void {
432+
firebase.analytics.setAnalyticsCollectionEnabled(true);
433+
alert({
434+
title: "Analytics collection",
435+
message: "ENABLED",
436+
okButtonText: "OK"
437+
});
438+
}
439+
440+
public doDisableAnalytics(): void {
441+
firebase.analytics.setAnalyticsCollectionEnabled(false);
442+
alert({
443+
title: "Analytics collection",
444+
message: "DISABLED",
445+
okButtonText: "OK"
446+
});
447+
}
448+
430449
public doLogAnalyticsEvent(): void {
431450
firebase.analytics.logEvent({
432451
// see https://firebase.google.com/docs/reference/android/com/google/firebase/analytics/FirebaseAnalytics.Event.html

docs/ANALYTICS.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,21 @@ Want to log a user ID with your analytics data? Go ahead:
8181
}
8282
);
8383
```
84+
85+
### analytics.setAnalyticsCollectionEnabled
86+
By default Firebase collects analytics data. If you don't want this (or first get user consent),
87+
you can disable/enable this at runtime:
88+
89+
```js
90+
firebase.analytics.setAnalyticsCollectionEnabled(false);
91+
```
92+
93+
You can also pass this property during `init()`:
94+
95+
```typescript
96+
firebase.init({
97+
analyticsCollectionEnabled: false
98+
});
99+
```
100+
101+
> If you want to disable collection without calling this function programmatically, then you can add a flag to your `App_Resources/iOS/Info.plist` and `App_Resources/Android/AndroidManifest.xml`, see [Firebase's documentation](https://firebase.google.com/support/guides/disable-analytics) for details.

src/analytics/analytics.android.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function logEvent(options: LogEventOptions): Promise<void> {
2222
}
2323

2424
com.google.firebase.analytics.FirebaseAnalytics.getInstance(
25-
appModule.android.currentContext || com.tns.NativeScriptApplication.getInstance()
25+
appModule.android.currentContext || com.tns.NativeScriptApplication.getInstance()
2626
).logEvent(options.key, bundle);
2727

2828
resolve();
@@ -64,7 +64,7 @@ export function setUserProperty(options: SetUserPropertyOptions): Promise<void>
6464
}
6565

6666
com.google.firebase.analytics.FirebaseAnalytics.getInstance(
67-
appModule.android.currentContext || com.tns.NativeScriptApplication.getInstance()
67+
appModule.android.currentContext || com.tns.NativeScriptApplication.getInstance()
6868
).setUserProperty(options.key, options.value);
6969

7070
resolve();
@@ -84,7 +84,7 @@ export function setScreenName(options: SetScreenNameOptions): Promise<void> {
8484
}
8585

8686
com.google.firebase.analytics.FirebaseAnalytics.getInstance(
87-
appModule.android.currentContext || com.tns.NativeScriptApplication.getInstance()
87+
appModule.android.currentContext || com.tns.NativeScriptApplication.getInstance()
8888
).setCurrentScreen(appModule.android.foregroundActivity, options.screenName, null);
8989

9090
resolve();
@@ -94,3 +94,9 @@ export function setScreenName(options: SetScreenNameOptions): Promise<void> {
9494
}
9595
});
9696
}
97+
98+
export function setAnalyticsCollectionEnabled(enabled: boolean): void {
99+
com.google.firebase.analytics.FirebaseAnalytics.getInstance(
100+
appModule.android.currentContext || com.tns.NativeScriptApplication.getInstance()
101+
).setAnalyticsCollectionEnabled(enabled);
102+
}

src/analytics/analytics.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,5 @@ export declare function setUserId(options: SetUserIdOptions): Promise<void>;
4343
export declare function setUserProperty(options: SetUserPropertyOptions): Promise<void>;
4444

4545
export declare function setScreenName(options: SetScreenNameOptions): Promise<void>;
46+
47+
export declare function setAnalyticsCollectionEnabled(enabled: boolean): void;

src/analytics/analytics.ios.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,7 @@ export function setScreenName(options: SetScreenNameOptions): Promise<void> {
8585
}
8686
});
8787
}
88+
89+
export function setAnalyticsCollectionEnabled(enabled: boolean): void {
90+
FIRAnalyticsConfiguration.sharedInstance().setAnalyticsCollectionEnabled(enabled);
91+
}

src/firebase.android.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@ firebase.init = arg => {
261261
const runInit = () => {
262262
arg = arg || {};
263263

264+
com.google.firebase.analytics.FirebaseAnalytics.getInstance(
265+
appModule.android.currentContext || com.tns.NativeScriptApplication.getInstance()
266+
).setAnalyticsCollectionEnabled(arg.analyticsCollectionEnabled || false);
267+
264268
if (typeof (com.google.firebase.database) !== "undefined" && typeof (com.google.firebase.database.ServerValue) !== "undefined") {
265269
firebase.ServerValue = {
266270
TIMESTAMP: firebase.toJsObject(com.google.firebase.database.ServerValue.TIMESTAMP)

src/firebase.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ export enum ServerValue {
7979
* The options object passed into the init function.
8080
*/
8181
export interface InitOptions {
82+
/**
83+
* Allow the app to send analytics data to Firebase.
84+
* Can also be set later with analytics.setAnalyticsCollectionEnabled.
85+
* Default true.
86+
*/
87+
analyticsCollectionEnabled?: boolean;
8288
/**
8389
* Allow disk persistence. Default true for Firestore, false for regular Firebase DB.
8490
*/

src/firebase.ios.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,8 @@ firebase.init = arg => {
341341
FIROptions.defaultOptions().deepLinkURLScheme = iOSUtils.getter(NSBundle, NSBundle.mainBundle).bundleIdentifier;
342342
}
343343

344+
FIRAnalyticsConfiguration.sharedInstance().setAnalyticsCollectionEnabled(arg.analyticsCollectionEnabled || false);
345+
344346
if (!firebase._configured) {
345347
firebase._configured = true;
346348
FIRApp.configure();

0 commit comments

Comments
 (0)