diff --git a/appcenter-analytics/CHANGELOG.md b/appcenter-analytics/CHANGELOG.md index c3aa9ff..b6eb2c6 100644 --- a/appcenter-analytics/CHANGELOG.md +++ b/appcenter-analytics/CHANGELOG.md @@ -1,5 +1,9 @@ # Change Log +## 0.3.0 + +* add support for Android + ## 0.2.1 * Update `Appcenter/Analytics` to 4.2.0 diff --git a/appcenter-analytics/README.md b/appcenter-analytics/README.md index 5db0e6d..b77831f 100644 --- a/appcenter-analytics/README.md +++ b/appcenter-analytics/README.md @@ -40,7 +40,7 @@ Once you add App Center Analytics to your app and the SDK is started, it will au -* [`enable(...)`](#enable) +* [`setEnabled(...)`](#setenabled) * [`isEnabled()`](#isenabled) * [`pause()`](#pause) * [`resume()`](#resume) @@ -52,19 +52,19 @@ Once you add App Center Analytics to your app and the SDK is started, it will au -### enable(...) +### setEnabled(...) ```typescript -enable(options: { enableFlag: boolean; }) => Promise +setEnabled(options: { enable: boolean; }) => Promise ``` You can enable and disable App Center Analytics at runtime. If you disable it, the SDK won't collect any more analytics information for the app. -| Param | Type | -| ------------- | ------------------------------------- | -| **`options`** | { enableFlag: boolean; } | +| Param | Type | +| ------------- | --------------------------------- | +| **`options`** | { enable: boolean; } | -**Since:** 0.1.0 +**Since:** 0.3.0 -------------------- diff --git a/appcenter-analytics/android/build.gradle b/appcenter-analytics/android/build.gradle index f0f8827..f06a9bb 100644 --- a/appcenter-analytics/android/build.gradle +++ b/appcenter-analytics/android/build.gradle @@ -1,8 +1,8 @@ ext { - junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.12' - androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.1.0' - androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.1.1' - androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.2.0' + junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.1' + androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.2.0' + androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.1.2' + androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.3.0' } buildscript { @@ -11,7 +11,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:4.1.1' + classpath 'com.android.tools.build:gradle:4.2.1' } } @@ -49,6 +49,8 @@ repositories { dependencies { + api 'com.microsoft.appcenter:appcenter-analytics:4.2.0' + api 'com.microsoft.appcenter.reactnative:appcenter-react-native:4.2.0' // change to own version implementation fileTree(dir: 'libs', include: ['*.jar']) implementation project(':capacitor-android') implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion" diff --git a/appcenter-analytics/android/gradle/wrapper/gradle-wrapper.properties b/appcenter-analytics/android/gradle/wrapper/gradle-wrapper.properties index 186b715..3c4101c 100644 --- a/appcenter-analytics/android/gradle/wrapper/gradle-wrapper.properties +++ b/appcenter-analytics/android/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.0-all.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/appcenter-analytics/android/src/main/AndroidManifest.xml b/appcenter-analytics/android/src/main/AndroidManifest.xml index b8df3c2..149a82b 100644 --- a/appcenter-analytics/android/src/main/AndroidManifest.xml +++ b/appcenter-analytics/android/src/main/AndroidManifest.xml @@ -1,3 +1,3 @@ + package="com.getcapacitor.plugin.appcenter.analytics"> diff --git a/appcenter-analytics/android/src/main/java/com/getcapacitor/plugin/appcenter/analytics/AnalyticsBase.java b/appcenter-analytics/android/src/main/java/com/getcapacitor/plugin/appcenter/analytics/AnalyticsBase.java new file mode 100644 index 0000000..e51db21 --- /dev/null +++ b/appcenter-analytics/android/src/main/java/com/getcapacitor/plugin/appcenter/analytics/AnalyticsBase.java @@ -0,0 +1,52 @@ +package com.getcapacitor.plugin.appcenter.analytics; + +import java.util.Map; + +import com.microsoft.appcenter.AppCenter; +import com.microsoft.appcenter.analytics.Analytics; +import com.microsoft.appcenter.Flags; + +public class AnalyticsBase { + + public void trackEvent(String name, Map properties, String flag) { + int analyticsFlag; + + switch (flag) { + case "critical": + analyticsFlag = Flags.CRITICAL; + break; + case "normal": + analyticsFlag = Flags.NORMAL; + break; + default: + analyticsFlag = Flags.NORMAL; + break; + } + Analytics.trackEvent(name, properties, analyticsFlag); + } + + public void setTransmissionInterval(int seconds) { + Analytics.setTransmissionInterval(seconds); + } + + public void start() { + AppCenter.start(Analytics.class); + } + + public void pause() { + Analytics.pause(); + } + + public void resume() { + Analytics.resume(); + } + + public void enable(boolean enable) { + Analytics.setEnabled(enable).get(); + } + + public boolean isEnabled() { + return Analytics.isEnabled().get(); + } + +} diff --git a/appcenter-analytics/android/src/main/java/com/getcapacitor/plugin/appcenter/analytics/AnalyticsPlugin.java b/appcenter-analytics/android/src/main/java/com/getcapacitor/plugin/appcenter/analytics/AnalyticsPlugin.java new file mode 100644 index 0000000..bd303f4 --- /dev/null +++ b/appcenter-analytics/android/src/main/java/com/getcapacitor/plugin/appcenter/analytics/AnalyticsPlugin.java @@ -0,0 +1,118 @@ +package com.getcapacitor.plugin.appcenter.analytics; + +import org.json.JSONArray; +import org.json.JSONObject; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import com.getcapacitor.JSObject; +import com.getcapacitor.Plugin; +import com.getcapacitor.PluginCall; +import com.getcapacitor.PluginMethod; +import com.getcapacitor.annotation.CapacitorPlugin; + +import com.microsoft.appcenter.AppCenter; +import com.microsoft.appcenter.analytics.Analytics; +import com.microsoft.appcenter.reactnative.shared.AppCenterReactNativeShared; + +@CapacitorPlugin(name = "Analytics") +public class AnalyticsPlugin extends Plugin { + + private AnalyticsBase implementation = new AnalyticsBase(); + + @Override + public void load() { + AppCenterReactNativeShared.configureAppCenter(this.getActivity().getApplication()); + + // todo: get additional config options + + if (AppCenter.isConfigured()) { + AppCenter.start(Analytics.class); + // if (!startEnabled) { + // Analytics.setEnabled(false); + // } + } + } + + @PluginMethod(returnType = PluginMethod.RETURN_NONE) + public void setEnabled(PluginCall call) { + implementation.enable(call.getBoolean("enable", false)); + call.resolve(); + } + + @PluginMethod(returnType = PluginMethod.RETURN_NONE) + public void pause(PluginCall call) { + implementation.pause(); + call.resolve(); + } + + @PluginMethod(returnType = PluginMethod.RETURN_NONE) + public void resume(PluginCall call) { + implementation.resume(); + call.resolve(); + } + + @PluginMethod + public void isEnabled(PluginCall call) { + JSObject ret = new JSObject(); + ret.put("value", implementation.isEnabled()); + call.resolve(ret); + } + + @PluginMethod(returnType = PluginMethod.RETURN_NONE) + public void trackEvent(PluginCall call) { + if (!call.getData().has("name")) { + call.reject("Must provide an event name"); + return; + } + String name = call.getString("name"); + + String flag = call.getString("flag", "normal"); + + Map properties = mapFromJSON(call.getObject("properties", new JSObject())); + + implementation.trackEvent(name, properties, flag); + } + + private static Map mapFromJSON(JSONObject jsonObject) { + if (jsonObject == null) { + return null; + } + Map map = new HashMap<>(); + Iterator keysIter = jsonObject.keys(); + while (keysIter.hasNext()) { + String key = keysIter.next(); + // Only support storing strings. Non-string data must be stringified in JS. + String value = jsonObject.optString(key); + if (value != null) { + map.put(key, value); + } + } + return map; + } + + private static Object getObject(Object value) { + if (value instanceof JSONObject) { + value = mapFromJSON((JSONObject) value); + } else if (value instanceof JSONArray) { + value = listFromJSON((JSONArray) value); + } + return value; + } + + private static List listFromJSON(JSONArray jsonArray) { + List list = new ArrayList<>(); + for (int i = 0, count = jsonArray.length(); i < count; i++) { + Object value = getObject(jsonArray.opt(i)); + if (value != null) { + list.add(value); + } + } + return list; + } + +} diff --git a/appcenter-analytics/android/src/main/java/com/mycompany/plugins/example/Analytics.java b/appcenter-analytics/android/src/main/java/com/mycompany/plugins/example/Analytics.java deleted file mode 100644 index 8398805..0000000 --- a/appcenter-analytics/android/src/main/java/com/mycompany/plugins/example/Analytics.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.mycompany.plugins.example; - -public class Analytics { - - public String echo(String value) { - return value; - } -} diff --git a/appcenter-analytics/android/src/main/java/com/mycompany/plugins/example/AnalyticsPlugin.java b/appcenter-analytics/android/src/main/java/com/mycompany/plugins/example/AnalyticsPlugin.java deleted file mode 100644 index 470a51e..0000000 --- a/appcenter-analytics/android/src/main/java/com/mycompany/plugins/example/AnalyticsPlugin.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.mycompany.plugins.example; - -import com.getcapacitor.JSObject; -import com.getcapacitor.Plugin; -import com.getcapacitor.PluginCall; -import com.getcapacitor.PluginMethod; -import com.getcapacitor.annotation.CapacitorPlugin; - -@CapacitorPlugin(name = "Analytics") -public class AnalyticsPlugin extends Plugin { - - private Analytics implementation = new Analytics(); - - @PluginMethod - public void echo(PluginCall call) { - String value = call.getString("value"); - - JSObject ret = new JSObject(); - ret.put("value", implementation.echo(value)); - call.resolve(ret); - } -} diff --git a/appcenter-analytics/package.json b/appcenter-analytics/package.json index 1244444..f88ccef 100644 --- a/appcenter-analytics/package.json +++ b/appcenter-analytics/package.json @@ -1,6 +1,6 @@ { "name": "@capacitor-community/appcenter-analytics", - "version": "0.2.1", + "version": "0.3.0", "description": "Capacitor plugin for AppCenter Analytics", "main": "dist/plugin.cjs.js", "module": "dist/esm/index.js", @@ -47,10 +47,10 @@ "prepublishOnly": "npm run build" }, "devDependencies": { - "@capacitor/android": "3.0.0", - "@capacitor/core": "3.0.0", + "@capacitor/android": "3.2.2", + "@capacitor/core": "3.2.2", "@capacitor/docgen": "^0.0.17", - "@capacitor/ios": "3.0.0", + "@capacitor/ios": "3.2.2", "@ionic/eslint-config": "^0.3.0", "@ionic/prettier-config": "^1.0.1", "@ionic/swiftlint-config": "^1.1.2", @@ -63,7 +63,7 @@ "typescript": "~4.0.3" }, "peerDependencies": { - "@capacitor/core": "next" + "@capacitor/core": "3.2.2" }, "prettier": "@ionic/prettier-config", "swiftlint": "@ionic/swiftlint-config", diff --git a/appcenter-analytics/src/definitions.ts b/appcenter-analytics/src/definitions.ts index 4733446..6609e7d 100644 --- a/appcenter-analytics/src/definitions.ts +++ b/appcenter-analytics/src/definitions.ts @@ -13,11 +13,11 @@ export interface AnalyticsEvent { export interface AnalyticsPlugin { /** * You can enable and disable App Center Analytics at runtime. If you disable it, the SDK won't collect any more analytics information for the app. - * @param {enableFlag: boolean} options + * @param {enable: boolean} options * @returns {Promise} - * @since 0.1.0 + * @since 0.3.0 */ - enable(options: {enableFlag: boolean}): Promise; + setEnabled(options: {enable: boolean}): Promise; /** * Check if Analytics is enabled or not. * @returns {Promise<{value: boolean}>} diff --git a/appcenter-analytics/src/web.ts b/appcenter-analytics/src/web.ts index 44940dd..d8146e3 100644 --- a/appcenter-analytics/src/web.ts +++ b/appcenter-analytics/src/web.ts @@ -3,6 +3,9 @@ import { WebPlugin } from '@capacitor/core'; import type { AnalyticsPlugin } from './definitions'; export class AnalyticsWeb extends WebPlugin implements AnalyticsPlugin { + setEnabled(): Promise { + throw this.unimplemented('Not supported on web.'); + } trackEvent(): Promise { throw this.unimplemented('Not supported on web.'); } @@ -15,7 +18,4 @@ export class AnalyticsWeb extends WebPlugin implements AnalyticsPlugin { isEnabled(): Promise<{ value: boolean; }> { throw this.unimplemented('Not supported on web.'); } - enable(): Promise { - throw this.unimplemented('Not supported on web.'); - } }