-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from capacitor-community/analytics-android-sup…
…port Analytics android support
- Loading branch information
Showing
12 changed files
with
201 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
# Change Log | ||
|
||
## 0.3.0 | ||
|
||
* add support for Android | ||
|
||
## 0.2.1 | ||
|
||
* Update `Appcenter/Analytics` to 4.2.0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
appcenter-analytics/android/gradle/wrapper/gradle-wrapper.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.mycompany.plugins.example"> | ||
package="com.getcapacitor.plugin.appcenter.analytics"> | ||
</manifest> |
52 changes: 52 additions & 0 deletions
52
...tics/android/src/main/java/com/getcapacitor/plugin/appcenter/analytics/AnalyticsBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, String> 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(); | ||
} | ||
|
||
} |
118 changes: 118 additions & 0 deletions
118
...cs/android/src/main/java/com/getcapacitor/plugin/appcenter/analytics/AnalyticsPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, String> properties = mapFromJSON(call.getObject("properties", new JSObject())); | ||
|
||
implementation.trackEvent(name, properties, flag); | ||
} | ||
|
||
private static Map<String, String> mapFromJSON(JSONObject jsonObject) { | ||
if (jsonObject == null) { | ||
return null; | ||
} | ||
Map<String, String> map = new HashMap<>(); | ||
Iterator<String> 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<Object> listFromJSON(JSONArray jsonArray) { | ||
List<Object> 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; | ||
} | ||
|
||
} |
8 changes: 0 additions & 8 deletions
8
appcenter-analytics/android/src/main/java/com/mycompany/plugins/example/Analytics.java
This file was deleted.
Oops, something went wrong.
22 changes: 0 additions & 22 deletions
22
appcenter-analytics/android/src/main/java/com/mycompany/plugins/example/AnalyticsPlugin.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters