Skip to content

Commit

Permalink
Support disabling of data collection (analytics/performance/crashlyti…
Browse files Browse the repository at this point in the history
…cs) at app startup and manual enabling of these at runtime.

Resolves dpa99c#116.
Resolves dpa99c#79.
  • Loading branch information
dpa99c committed Sep 3, 2019
1 parent e4eca8c commit 92a1740
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 83 deletions.
96 changes: 66 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,18 @@ To help ensure this plugin is kept updated, new features are added and bugfixes
- [Ionic 4](#ionic-4)
- [Ionic 3](#ionic-3)
- [Build environment notes](#build-environment-notes)
- [Specifying dependent library versions](#specifying-dependent-library-versions)
- [PhoneGap Build](#phonegap-build)
- [Android-specific](#android-specific)
- [Specifying dependent library versions](#specifying-dependent-library-versions)
- [AndroidX](#androidx)
- [Google Play Services and Firebase libraries](#google-play-services-and-firebase-libraries)
- [iOS-specific](#ios-specific)
- [Specifying dependent library versions](#specifying-dependent-library-versions-1)
- [Cocoapods](#cocoapods)
- [Out-of-date pods](#out-of-date-pods)
- [Strip debug symbols](#strip-debug-symbols)
- [Firebase config setup](#firebase-config-setup)
- [Disable data collection on startup](#disable-data-collection-on-startup)
- [Example project](#example-project)
- [Reporting issues](#reporting-issues)
- [Cloud messaging](#cloud-messaging)
Expand Down Expand Up @@ -87,6 +89,7 @@ To help ensure this plugin is kept updated, new features are added and bugfixes
- [setUserId](#setuserid)
- [setUserProperty](#setuserproperty)
- [Crashlytics](#crashlytics)
- [setCrashlyticsCollectionEnabled](#setcrashlyticscollectionenabled)
- [setCrashlyticsUserId](#setcrashlyticsuserid)
- [sendCrash](#sendcrash)
- [logMessage](#logmessage)
Expand All @@ -104,6 +107,7 @@ To help ensure this plugin is kept updated, new features are added and bugfixes
- [setConfigSettings](#setconfigsettings)
- [setDefaults](#setdefaults)
- [Performance](#performance)
- [setPerformanceCollectionEnabled](#setperformancecollectionenabled)
- [startTrace](#starttrace)
- [incrementCounter](#incrementcounter)
- [stopTrace](#stoptrace)
Expand Down Expand Up @@ -196,13 +200,18 @@ If you want to make the `onMessageReceived()` JS API behave like the Ionic Nativ

## Build environment notes

### Specifying dependent library versions
This plugin depends on various components such as the Firebase SDK which are pulled in at build-time by Gradle on Android and Cocoapods on iOS.
By default this plugin pins specific versions of these in [its `plugin.xml`](https://github.com/dpa99c/cordova-plugin-firebase/blob/master/plugin.xml) where you can find the currently pinned Android & iOS versions as `<preference>`'s, for example:
### PhoneGap Build
This plugin will not work with Phonegap Build (and other remote cloud build envs) do not support Cordova hook scripts as they are used by this plugin to configure the native platform projects.

### Android-specific

#### Specifying dependent library versions
This plugin depends on various components such as the Firebase SDK which are pulled in at build-time by Gradle on Android.
By default this plugin pins specific versions of these in [its `plugin.xml`](https://github.com/dpa99c/cordova-plugin-firebase/blob/master/plugin.xml) where you can find the currently pinned versions as `<preference>`'s, for example:

<preference name="ANDROID_FIREBASE_CORE_VERSION" default="17.0.0" />

These defaults can be overridden at plugin installation time by specifying plugin variables as command-line arguments, for example:
The Android defaults can be overridden at plugin installation time by specifying plugin variables as command-line arguments, for example:

cordova plugin add cordova-plugin-firebasex --variable ANDROID_FIREBASE_CORE_VERSION=17.0.0

Expand All @@ -224,16 +233,6 @@ The following plugin variables are use to specify the follow Gradle dependency v
- `ANDROID_CRASHLYTICS_NDK_VERSION` => `com.crashlytics.sdk.android:crashlytics-ndk`
- `ANDROID_SHORTCUTBADGER_VERSION` => `me.leolin:ShortcutBadger`

The following plugin variables are use to specify the follow Cocoapods dependency versions on iOS:

- `IOS_FIREBASE_CORE_VERSION` => `Firebase/Core`
- `IOS_FIREBASE_AUTH_VERSION` => `Firebase/Auth`
- `IOS_FIREBASE_MESSAGING_VERSION` => `Firebase/Messaging`
- `IOS_FIREBASE_PERFORMANCE_VERSION` => `Firebase/Performance`
- `IOS_FIREBASE_REMOTECONFIG_VERSION` => `Firebase/RemoteConfig`
- `IOS_FABRIC_VERSION` => `Fabric`
- `IOS_CRASHLYTICS_VERSION` => `Crashlytics`

For example, to explicitly specify all the component versions at plugin install time:

cordova plugin add cordova-plugin-firebasex \
Expand All @@ -246,18 +245,7 @@ For example, to explicitly specify all the component versions at plugin install
--variable ANDROID_CRASHLYTICS_VERSION=2.10.1 \
--variable ANDROID_CRASHLYTICS_NDK_VERSION=2.1.0 \
--variable ANDROID_SHORTCUTBADGER_VERSION=1.1.22 \
--variable IOS_FIREBASE_CORE_VERSION=5.20.2 \
--variable IOS_FIREBASE_AUTH_VERSION=5.20.2 \
--variable IOS_FIREBASE_MESSAGING_VERSION=5.20.2 \
--variable IOS_FIREBASE_PERFORMANCE_VERSION=5.20.2 \
--variable IOS_FIREBASE_REMOTECONFIG_VERSION=5.20.2 \
--variable IOS_FABRIC_VERSION=1.9.0 \
--variable IOS_CRASHLYTICS_VERSION=3.12.0

### PhoneGap Build
This plugin will not work with Phonegap Build (and other remote cloud build envs) do not support Cordova hook scripts as they are used by this plugin to configure the native platform projects.
### Android-specific
#### AndroidX
This plugin has been migrated to use [AndroidX (Jetpack)](https://developer.android.com/jetpack/androidx/migrate) which is the successor to the [Android Support Library](https://developer.android.com/topic/libraries/support-library/index).
This is implemented by adding a dependency on [cordova-plugin-androidx](https://github.com/dpa99c/cordova-plugin-androidx) which enables AndroidX in the Android platform of a Cordova project.
Expand All @@ -276,6 +264,15 @@ Similarly, if your build is failing because multiple plugins are installing diff
you can try installing [cordova-android-firebase-gradle-release](https://github.com/dpa99c/cordova-android-firebase-gradle-release) to align these.

### iOS-specific
#### Specifying dependent library versions
This plugin depends on various components such as the Firebase SDK which are pulled in at build-time by Cocoapods on iOS.
This plugin pins specific versions of these in [its `plugin.xml`](https://github.com/dpa99c/cordova-plugin-firebase/blob/master/plugin.xml) where you can find the currently pinned iOS versions in the `<pod>`'s, for example:

<pod name="Firebase/Core" spec="6.3.0"/>

**It is currently not possible to override these at plugin installation time** because `cordova@9`/`cordova-ios@5` does not support the use of plugin variables in the `<pod>`'s `spec` attribute.
Therefore if you need to change the specified versions, you'll currently need to do this by forking the plugin and editing the `plugin.xml` to change the specified `spec` values.

#### Cocoapods
This plugin relies on `cordova@9`/`cordova-ios@5` support for the [CocoaPods dependency manager]( https://cocoapods.org/) in order to satisfy the iOS Firebase SDK library dependencies.

Expand Down Expand Up @@ -326,9 +323,30 @@ Check out this [firebase article](https://support.google.com/firebase/answer/701
GoogleService-Info.plist <--
...
```

IMPORTANT: The Firebase SDK requires the configuration files to be present and valid, otherwise your app will crash on boot or Firebase features won't work.

## Disable data collection on startup
By default, analytics, performance and Crashlytics data will begin being collected as soon as the app starts up.
However, for data protection or privacy reasons, you may wish to disable data collection until such time as the user has granted their permission.

To do this, set the following plugin variables to `false` at plugin install time:

* `FIREBASE_ANALYTICS_COLLECTION_ENABLED`
* `FIREBASE_PERFORMANCE_COLLECTION_ENABLED`
* `FIREBASE_CRASHLYTICS_COLLECTION_ENABLED`


cordova plugin add cordova-plugin-firebasex \
--variable FIREBASE_ANALYTICS_COLLECTION_ENABLED=false \
--variable FIREBASE_PERFORMANCE_COLLECTION_ENABLED=false \
--variable FIREBASE_CRASHLYTICS_COLLECTION_ENABLED=false

This will disable data collection (on both Android & iOS) until you call [setAnalyticsCollectionEnabled](#setanalyticscollectionenabled), [setPerformanceCollectionEnabled](#setperformancecollectionenabled) and [setCrashlyticsCollectionEnabled](#setcrashlyticscollectionenabled):

window.FirebasePlugin.setAnalyticsCollectionEnabled(true);
window.FirebasePlugin.setPerformanceCollectionEnabled(true);
window.FirebasePlugin.setCrashlyticsCollectionEnabled();

## Example project
An example project repo exists to demonstrate and validate the functionality of this plugin:
https://github.com/dpa99c/cordova-plugin-firebasex-test
Expand Down Expand Up @@ -1212,12 +1230,12 @@ The easiest way to set this up is by [streaming Firebase Analytics data into Big
Note that until you set this up, all fine-grain event-level data is discarded by Firebase.

#### setAnalyticsCollectionEnabled
Enable/disable analytics collection (useful for GDPR/privacy settings).
Manually enable/disable analytics data collection, e.g. if [disabled on app startup](#disable-data-collection-on-startup).

```
window.FirebasePlugin.setAnalyticsCollectionEnabled(true); // Enables analytics collection
window.FirebasePlugin.setAnalyticsCollectionEnabled(true); // Enables analytics data collection
window.FirebasePlugin.setAnalyticsCollectionEnabled(false); // Disables analytics collection
window.FirebasePlugin.setAnalyticsCollectionEnabled(false); // Disables analytics data collection
```

#### logEvent
Expand Down Expand Up @@ -1249,6 +1267,15 @@ By default this plugin will ensure fatal native crashes in your apps are reporte

Note that for iOS you may need to [upload the dSYM](https://firebase.google.com/docs/crashlytics/get-deobfuscated-reports?authuser=0) for you build before crashes are displayed in the Firebase console.

#### setCrashlyticsCollectionEnabled
Manually enable Crashlytics data collection if [disabled on app startup](#disable-data-collection-on-startup).

```
window.FirebasePlugin.setCrashlyticsCollectionEnabled();
```

Note: once enabled, Crashlytics data collection cannot be disabled during the app session.

#### setCrashlyticsUserId
Set Crashlytics user identifier.

Expand Down Expand Up @@ -1465,6 +1492,15 @@ window.FirebasePlugin.setDefaults(defaults);

### Performance

#### setPerformanceCollectionEnabled
Manually enable/disable performance data collection, e.g. if [disabled on app startup](#disable-data-collection-on-startup).

```
window.FirebasePlugin.setPerformanceCollectionEnabled(true); // Enables performance data collection
window.FirebasePlugin.setPerformanceCollectionEnabled(false); // Disables performance data collection
```

#### startTrace

Start a trace.
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"dependencies": {
"xcode": "^2.0.0",
"xml2js": "^0.4.16",
"q": "^1.5.1"
"q": "^1.5.1",
"plist": "^3.0.1"
}
}
7 changes: 7 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<engine name="cordova-ios" version=">=5" />
</engines>

<preference name="FIREBASE_ANALYTICS_COLLECTION_ENABLED" default="true" />
<preference name="FIREBASE_PERFORMANCE_COLLECTION_ENABLED" default="true" />
<preference name="FIREBASE_CRASHLYTICS_COLLECTION_ENABLED" default="true" />

<platform name="android">
<js-module name="FirebasePlugin" src="www/firebase.js">
<clobbers target="FirebasePlugin" />
Expand All @@ -40,6 +44,9 @@
<receiver android:name="org.apache.cordova.firebase.OnNotificationOpenReceiver"/>
<meta-data android:name="com.google.firebase.messaging.default_notification_color" android:resource="@color/accent" />
<meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="@string/default_notification_channel_id"/>
<meta-data android:name="firebase_analytics_collection_enabled" android:value="$FIREBASE_ANALYTICS_COLLECTION_ENABLED" />
<meta-data android:name="firebase_performance_collection_enabled" android:value="$FIREBASE_PERFORMANCE_COLLECTION_ENABLED" />
<meta-data android:name="firebase_crashlytics_collection_enabled" android:value="$FIREBASE_CRASHLYTICS_COLLECTION_ENABLED" />
</config-file>
<resource-file src="src/android/cordova-plugin-firebase-strings.xml" target="res/values/cordova-plugin-firebase-strings.xml" />
<source-file src="src/android/FirebasePlugin.java" target-dir="src/org/apache/cordova/firebase" />
Expand Down
15 changes: 6 additions & 9 deletions scripts/after_prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,16 @@ var PLUGIN_ID = 'cordova-plugin-firebasex';

var PLATFORM = {
IOS: {
dest: [
IOS_DIR + '/' + name + '/Resources/GoogleService-Info.plist',
IOS_DIR + '/' + name + '/Resources/Resources/GoogleService-Info.plist'
],
dest: IOS_DIR + '/' + name + '/Resources/GoogleService-Info.plist',
src: [
'GoogleService-Info.plist',
IOS_DIR + '/www/GoogleService-Info.plist',
'www/GoogleService-Info.plist'
]
],
appPlist: IOS_DIR + '/' + name + '/'+name+'-Info.plist',
},
ANDROID: {
dest: [
ANDROID_DIR + '/google-services.json',
ANDROID_DIR + '/app/google-services.json'
],
dest: ANDROID_DIR + '/app/google-services.json',
src: [
'google-services.json',
ANDROID_DIR + '/assets/www/google-services.json',
Expand Down Expand Up @@ -114,6 +109,8 @@ module.exports = function (context) {
if(pluginVariables['IOS_STRIP_DEBUG'] && pluginVariables['IOS_STRIP_DEBUG'] === 'true'){
helper.stripDebugSymbols();
}
helper.applyPluginVarsToPlists(PLATFORM.IOS.dest, PLATFORM.IOS.appPlist, pluginVariables);

deferred.resolve();
}).catch(error => {
deferred.reject(error);
Expand Down
24 changes: 24 additions & 0 deletions scripts/ios/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var fs = require("fs");
var path = require("path");
var utilities = require("../lib/utilities");
var xcode = require("xcode");
var plist = require('plist');

/**
* This is used as the display text for the build phase block in XCode as well as the
Expand Down Expand Up @@ -181,5 +182,28 @@ module.exports = {
fs.writeFileSync(podFilePath, podFile);
console.log('cordova-plugin-firebasex: Applied IOS_STRIP_DEBUG to Podfile');
}
},
applyPluginVarsToPlists: function(googlePlistPath, appPlistPath, pluginVariables){
var googlePlist = plist.parse(fs.readFileSync(googlePlistPath, 'utf8')),
appPlist = plist.parse(fs.readFileSync(appPlistPath, 'utf8')),
googlePlistModified = false,
appPlistModified = false;

if(typeof pluginVariables['FIREBASE_ANALYTICS_COLLECTION_ENABLED'] !== 'undefined'){
googlePlist["FIREBASE_ANALYTICS_COLLECTION_ENABLED"] = (pluginVariables['FIREBASE_ANALYTICS_COLLECTION_ENABLED'] !== "false" ? "true" : "false") ;
appPlist["FirebaseScreenReportingEnabled"] = (pluginVariables['FIREBASE_ANALYTICS_COLLECTION_ENABLED'] !== "false");
appPlistModified = googlePlistModified = true;
}
if(typeof pluginVariables['FIREBASE_PERFORMANCE_COLLECTION_ENABLED'] !== 'undefined'){
googlePlist["FIREBASE_PERFORMANCE_COLLECTION_ENABLED"] = (pluginVariables['FIREBASE_PERFORMANCE_COLLECTION_ENABLED'] !== "false" ? "true" : "false") ;
googlePlistModified = true;
}
if(typeof pluginVariables['FIREBASE_CRASHLYTICS_COLLECTION_ENABLED'] !== 'undefined'){
googlePlist["FIREBASE_CRASHLYTICS_COLLECTION_ENABLED"] = (pluginVariables['FIREBASE_CRASHLYTICS_COLLECTION_ENABLED'] !== "false" ? "true" : "false") ;
googlePlistModified = true;
}

if(googlePlistModified) fs.writeFileSync(googlePlistPath, plist.build(googlePlist));
if(appPlistModified) fs.writeFileSync(appPlistPath, plist.build(appPlist));
}
};
3 changes: 1 addition & 2 deletions scripts/lib/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@ module.exports = {
var contents = fs.readFileSync(file).toString();

try {
platform.dest.forEach(function (destinationPath) {
var destinationPath = platform.dest;
var folder = destinationPath.substring(0, destinationPath.lastIndexOf('/'));
fs.ensureDirSync(folder);
fs.writeFileSync(destinationPath, contents);
});
} catch (e) {
// skip
}
Expand Down
22 changes: 19 additions & 3 deletions src/android/FirebasePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.google.firebase.perf.FirebasePerformance;
import com.google.firebase.perf.metrics.Trace;

import io.fabric.sdk.android.Fabric;
import me.leolin.shortcutbadger.ShortcutBadger;

import org.apache.cordova.CallbackContext;
Expand Down Expand Up @@ -91,7 +92,6 @@ public void run() {
Log.d(TAG, "Starting Firebase plugin");
FirebaseApp.initializeApp(applicationContext);
mFirebaseAnalytics = FirebaseAnalytics.getInstance(applicationContext);
mFirebaseAnalytics.setAnalyticsCollectionEnabled(true);
if (extras != null && extras.size() > 1) {
if (FirebasePlugin.notificationStack == null) {
FirebasePlugin.notificationStack = new ArrayList<Bundle>();
Expand Down Expand Up @@ -207,6 +207,9 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
} else if (action.equals("setPerformanceCollectionEnabled")) {
this.setPerformanceCollectionEnabled(callbackContext, args.getBoolean(0));
return true;
} else if (action.equals("setCrashlyticsCollectionEnabled")) {
this.setCrashlyticsCollectionEnabled(callbackContext);
return true;
} else if (action.equals("clearAllNotifications")) {
this.clearAllNotifications(callbackContext);
return true;
Expand Down Expand Up @@ -967,7 +970,6 @@ public void run() {
}

private void setAnalyticsCollectionEnabled(final CallbackContext callbackContext, final boolean enabled) {
final FirebasePlugin self = this;
cordova.getThreadPool().execute(new Runnable() {
public void run() {
try {
Expand All @@ -982,7 +984,6 @@ public void run() {
}

private void setPerformanceCollectionEnabled(final CallbackContext callbackContext, final boolean enabled) {
final FirebasePlugin self = this;
cordova.getThreadPool().execute(new Runnable() {
public void run() {
try {
Expand All @@ -996,6 +997,21 @@ public void run() {
});
}

private void setCrashlyticsCollectionEnabled(final CallbackContext callbackContext) {
final FirebasePlugin self = this;
cordova.getThreadPool().execute(new Runnable() {
public void run() {
try {
Fabric.with(self.applicationContext, new Crashlytics());
callbackContext.success();
} catch (Exception e) {
handleExceptionWithContext(e, callbackContext);
e.printStackTrace();
}
}
});
}

public void clearAllNotifications(final CallbackContext callbackContext) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
Expand Down
1 change: 1 addition & 0 deletions src/ios/FirebasePlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
- (void)stopTrace:(CDVInvokedUrlCommand*)command;
- (void)setAnalyticsCollectionEnabled:(CDVInvokedUrlCommand*)command;
- (void)setPerformanceCollectionEnabled:(CDVInvokedUrlCommand*)command;
- (void)setCrashlyticsCollectionEnabled:(CDVInvokedUrlCommand*)command;
- (void)clearAllNotifications:(CDVInvokedUrlCommand *)command;
- (void)logMessage:(CDVInvokedUrlCommand*)command;
- (void)sendCrash:(CDVInvokedUrlCommand*)command;
Expand Down
Loading

0 comments on commit 92a1740

Please sign in to comment.