Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for opt-in reporting via manual SDK initialization. #134

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ It requires Cordova 6.x or newer (tested on 7.0.0) and has APIs for iOS and Andr

The iOS version uses Fabric SDK 1.7.5 and Crashlytics SDK 3.10.1 framework bundles which are located in `lib/ios`.

The Android version uses Gradle to get the Fabric SDK (`io.fabric.tools:gradle:1.+`) and the Crashlytics SDK (`com.crashlytics.sdk.android:crashlytics:2.9.1`) from Maven repositories when the plugin is added.
The Android version uses Gradle to get the Fabric SDK (`io.fabric.tools:gradle:1.+`) and the Crashlytics SDK (`com.crashlytics.sdk.android:crashlytics:2.9.3`) from Maven repositories when the plugin is added.

# Install

Expand Down Expand Up @@ -133,6 +133,32 @@ The desired package versions are pulled from [package.json](./package.json):
}
```

## Opt-in reporting

By default this plugin will automatically intialize the Fabric SDK on app startup and therefore assumes implicit user consent that information such as crash reports or analytics can be collected.

Under the [EU's GDPR rules](https://www.eugdpr.org/) you may need to ensure that your users have given consent.

This means you may need to [enable opt-in reporting](https://docs.fabric.io/apple/crashlytics/advanced-setup.html#enable-opt-in-reporting) to ask users consent before intializing the Fabric SDK which immediately begins reporting Answers and Crashlytics data.

This plugin can be configured for manual initialization by setting the `FABRIC_AUTO_INIT` plugin variable to `false` when the plugin is installed:
```bash
cordova plugin add cordova-fabric-plugin --variable FABRIC_AUTO_INIT=false --variable FABRIC_API_KEY=XXX --variable FABRIC_API_SECRET=xxx
```

This enables you to gain user consent before manually intializing the SDK by calling:
```javascript
window.fabric.core.initialize();
```

You can check if the plugin has already been initialized with:

```javascript
window.fabric.core.isInitialized(function(initialized){
console.log("Fabric SDK " + (initialized ? "is" : "is not") + " initialized");
});
```

# Contributing

If you wish to contribute please see `CONTRIBUTING.md`.
Expand Down
4 changes: 2 additions & 2 deletions hooks/lib/ios-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = {
addShellScriptBuildPhase: function (context, xcodeProjectPath) {

var pluginConfig = utilities.getPluginConfig("ios");
var xcode = context.requireCordovaModule("xcode");
var xcode = require("xcode");

// Read and parse the XCode project (.pxbproj) from disk.
// File format information: http://www.monobjc.net/xcode-project-file-format.html
Expand Down Expand Up @@ -75,7 +75,7 @@ module.exports = {
*/
removeShellScriptBuildPhase: function (context, xcodeProjectPath) {

var xcode = context.requireCordovaModule("xcode");
var xcode = require("xcode");

// Read and parse the XCode project (.pxbproj) from disk.
// File format information: http://www.monobjc.net/xcode-project-file-format.html
Expand Down
Binary file modified lib/ios/Crashlytics.framework/Crashlytics
Binary file not shown.
Binary file modified lib/ios/Crashlytics.framework/Info.plist
Binary file not shown.
Binary file modified lib/ios/Crashlytics.framework/submit
Binary file not shown.
Binary file modified lib/ios/Crashlytics.framework/uploadDSYM
Binary file not shown.
Binary file modified lib/ios/Fabric.framework/Fabric
Binary file not shown.
Binary file modified lib/ios/Fabric.framework/Info.plist
Binary file not shown.
Binary file modified lib/ios/Fabric.framework/uploadDSYM
Binary file not shown.
8 changes: 4 additions & 4 deletions lib/ios/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ https://cocoapods.org/pods/Crashlytics

Podspec references:

https://github.com/CocoaPods/Specs/blob/master/Specs/Fabric/1.6.11/Fabric.podspec.json
https://github.com/CocoaPods/Specs/blob/master/Specs/Crashlytics/3.8.5/Crashlytics.podspec.json
https://github.com/CocoaPods/Specs/blob/master/Specs/Fabric/1.8.2/Fabric.podspec.json
https://github.com/CocoaPods/Specs/blob/master/Specs/Crashlytics/3.11.1/Crashlytics.podspec.json

Framework package ZIPs:

https://kit-downloads.fabric.io/cocoapods/fabric/1.7.5/fabric.zip
https://kit-downloads.fabric.io/cocoapods/crashlytics/3.10.1/crashlytics.zip
https://kit-downloads.fabric.io/cocoapods/fabric/1.8.2/fabric.zip
https://kit-downloads.fabric.io/cocoapods/crashlytics/3.11.1/crashlytics.zip
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@
"type": "opencollective",
"url": "https://opencollective.com/FabricPlugin"
}
}
}
10 changes: 9 additions & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>

<plugin id="cordova-fabric-plugin" version="1.1.14-dev" xmlns="http://www.phonegap.com/ns/plugins/1.0">
<plugin
id="cordova-fabric-plugin"
version="1.1.14-dev"
xmlns="http://www.phonegap.com/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android">

<name>cordova-fabric-plugin</name>
<description>Cordova Fabric.io Plugin</description>
Expand All @@ -9,6 +13,7 @@

<preference name="FABRIC_API_KEY" />
<preference name="FABRIC_API_SECRET" />
<preference name="FABRIC_AUTO_INIT" default="true" />

<hook type="after_plugin_install" src="hooks/after_plugin_add.js" />
<hook type="after_platform_add" src="hooks/after_plugin_add.js" />
Expand Down Expand Up @@ -41,6 +46,7 @@

<config-file target="AndroidManifest.xml" parent="application">
<meta-data android:name="io.fabric.ApiKey" android:value="$FABRIC_API_KEY"/>
<meta-data android:name="FabricPlugin_AutoInit" android:value="$FABRIC_AUTO_INIT"/>
</config-file>

<source-file src="src/android/FabricPlugin.java" target-dir="src/com/sarriaroman/fabric"/>
Expand All @@ -62,6 +68,8 @@
<dict>
<key>APIKey</key>
<string>$FABRIC_API_KEY</string>
<key>AutoInit</key>
<string>$FABRIC_AUTO_INIT</string>
<key>Kits</key>
<array>
<dict>
Expand Down
44 changes: 42 additions & 2 deletions src/android/FabricPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
import com.crashlytics.android.answers.SignUpEvent;
import com.crashlytics.android.answers.StartCheckoutEvent;

import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;

import java.lang.reflect.Method;
Expand All @@ -36,17 +40,35 @@

public class FabricPlugin extends CordovaPlugin {
private final String pluginName = "FabricPlugin";
private final String autoInitKey = "FabricPlugin_AutoInit";

private boolean initialized = false;

@Override
protected void pluginInitialize() {
Fabric.with(this.cordova.getActivity().getApplicationContext(), new Crashlytics(), new Answers());
boolean autoInit = true;
try {
Activity activity = this.cordova.getActivity();
ApplicationInfo ai = activity.getPackageManager().getApplicationInfo(activity.getPackageName(), PackageManager.GET_META_DATA);
Bundle bundle = ai.metaData;
autoInit = bundle.getBoolean(autoInitKey);
} catch (Exception e) {
Log.e(pluginName, "Failed to load FABRIC_AUTO_INIT meta-data: " + e.getMessage());
}
if(autoInit){
initialize();
}
}

@Override
public boolean execute(final String action, final JSONArray data, final CallbackContext callbackContext) {
Log.d(pluginName, pluginName + " called with options: " + data);

if (action.equals("addLog")) {
if (action.equals("initialize")) {
initialize(data, callbackContext);
} else if (action.equals("isInitialized")) {
isInitialized(data, callbackContext);
} else if (action.equals("addLog")) {
addLog(data, callbackContext);
} else if (action.equals("sendCrash")) {
sendCrash(data, callbackContext);
Expand Down Expand Up @@ -101,6 +123,24 @@ public boolean execute(final String action, final JSONArray data, final Callback
return true;
}

private void initialize(final JSONArray data,
final CallbackContext callbackContext) {
initialize();
callbackContext.success();
}

private void initialize(){
if(!initialized){
Fabric.with(this.cordova.getActivity().getApplicationContext(), new Crashlytics(), new Answers());
initialized = true;
}
}

private void isInitialized(final JSONArray data,
final CallbackContext callbackContext) {
callbackContext.success(initialized ? 1 : 0);
}

/* Crashlytics Events */

private void addLog(final JSONArray data,
Expand Down
2 changes: 1 addition & 1 deletion src/android/build-extras.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ repositories {
}

dependencies {
compile('com.crashlytics.sdk.android:crashlytics:2.9.1@aar') {
compile('com.crashlytics.sdk.android:crashlytics:2.9.3@aar') {
transitive = true;
}
}
Expand Down
33 changes: 32 additions & 1 deletion src/ios/FabricPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

@interface FabricPlugin : CDVPlugin

@property (nonatomic) BOOL initialized;
// Fabric
-(void) initialize:(CDVInvokedUrlCommand *)command;
-(void) isInitialized:(CDVInvokedUrlCommand *)command;

// Answers
- (void)sendPurchase:(CDVInvokedUrlCommand*)command;
- (void)sendAddToCart:(CDVInvokedUrlCommand*)command;
Expand Down Expand Up @@ -43,11 +48,37 @@ - (void)sendNonFatalCrash:(CDVInvokedUrlCommand*)command;

@implementation FabricPlugin

@synthesize initialized;

#pragma mark - Plugin Initialization

- (void)pluginInitialize
{
[Fabric with:@[[Crashlytics class], [Answers class]]];
initialized = false;
NSDictionary* fabricSettings = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"Fabric"];
bool autoInit = [[fabricSettings objectForKey:@"AutoInit"] boolValue];
if(autoInit){
[self _initialize];
}
}

- (void) _initialize
{
if(!initialized){
[Fabric with:@[[Crashlytics class], [Answers class]]];
initialized = true;
}
}

-(void) initialize:(CDVInvokedUrlCommand *)command {
[self _initialize];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

-(void) isInitialized:(CDVInvokedUrlCommand *)command{
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:initialized];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

#pragma mark - Answers
Expand Down
17 changes: 17 additions & 0 deletions typings/cordova-fabric-plugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@
declare module FabricPlugin {

interface FabricPluginStatic {
/**
* Manually initializes the plugin.
* Only relevant if the plugin --variable FABRIC_AUTO_INIT=false
*
* https://docs.fabric.io/android/crashlytics/advanced-setup.html#enable-opt-in-reporting
* https://docs.fabric.io/apple/crashlytics/advanced-setup.html#enable-opt-in-reporting
*/
initialize: (success: () => void) => void;

/**
* Checks if the plugin is initialized.
* Only relevant if the plugin --variable FABRIC_AUTO_INIT=false
*
* https://docs.fabric.io/android/crashlytics/advanced-setup.html#enable-opt-in-reporting
* https://docs.fabric.io/apple/crashlytics/advanced-setup.html#enable-opt-in-reporting
*/
isInitialized: (success: (initialized: boolean) => void) => void;

/**
* API for interacting with the Crashlytics kit.
Expand Down
9 changes: 9 additions & 0 deletions www/FabricPlugin.es6
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ class FabricPlugin {
exec(null, null, this.pluginCallName(), method, parameters);
}

initialize(success) {
exec(success, null, this.pluginCallName(), "initialize", []);
}

isInitialized(success) {
exec(success, null, this.pluginCallName(), "isInitialized", []);
}

}


module.exports = new FabricPlugin();
10 changes: 10 additions & 0 deletions www/FabricPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ var FabricPlugin = (function () {
value: function execPlugin(method, parameters) {
exec(null, null, this.pluginCallName(), method, parameters);
}
}, {
key: 'initialize',
value: function initialize(success) {
exec(success, null, this.pluginCallName(), "initialize", []);
}
}, {
key: 'isInitialized',
value: function isInitialized(success) {
exec(success, null, this.pluginCallName(), "isInitialized", []);
}
}]);

return FabricPlugin;
Expand Down