Importing the Android SDK
Requirements
- Android 5.0(Lollipop) and above.
Installation
The Copilot.cx Android SDK is available via private maven repository, here few steps to follow in order to install successfully the Android SDK:
- In your project’s general
build.gradle
file,under theallProjects.repositories
add the following node:
allprojects {
repositories {
maven {
url "https://mymavenrepo.com/repo/i0DkV5KtDEuOB0P66JPC/"
credentials {
username 'myMavenRepo'
password 'yawetagtoa'
}
}
}
- In your app’s module
build.gradle
file, please add the following dependency (please refer the releases page for to get specific version):
implementation 'com.copilot:copilot-api-access:YOUR_COPILOT_ANDROID_SDK_VERSION_HERE'
💡If you are using support-library (or any other clashing dependency), please consider excluding Copilot’s transitive dependencies as shown below:
implementation ('com.copilot:copilot-api-access:YOUR_COPILOT_ANDROID_SDK_VERSION_HERE'){
exclude group: 'com.android.support', module: 'support-media-compat'
exclude group: 'com.android.support', module: 'animated-vector-drawable'
exclude group: 'com.android.support', module: 'support-v4'
}
- In your
AndroidManifest.xml
, add the Copilot.cx application configuration under the<application>
tag:
<meta-data
android:name="com.copilot.ENVIRONMENT_URL"
android:value="COPILOT_ENVIRONMENT_URL_HERE"/>
<meta-data
android:name="com.copilot.APPLICATION_ID"
android:value="YOUR_APPLICATION_ID_HERE"/>
<meta-data
android:name="com.copilot.IS_GDPR_COMPLIANT"
android:value="IS_APPLICATION_GDPR_COMPLIANT(true/false)"/>
<meta-data
android:name="com.copilot.MANAGE_TYPE"
android:value="Sphere"/>
Replace
COPILOT_ENVIRONMENT_URL_HERE
,YOUR_APPLICATION_ID_HERE
andIS_APPLICATION_GDPR_COMPLIANT
(true/false) with the appropriate values (please refer the SDK configuration section)Add the
FirebaseAnalyticsEventLogProvider
class implementing theEventLogProvider
interface to your project.
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import com.copilot.analytics.AnalyticsConstants;
import com.copilot.analytics.EventLogProvider;
import com.google.firebase.analytics.FirebaseAnalytics;
import java.util.Map;
public class FirebaseAnalyticsEventLogProvider implements EventLogProvider {
private static final String TAG = FirebaseAnalyticsEventLogProvider.class.getSimpleName();
@NonNull
private final Context mContext;
public FirebaseAnalyticsEventLogProvider(@NonNull Context context) {
mContext = context.getApplicationContext();
deactivate();
}
@Override
public String getProviderName() {
return TAG;
}
@Override
public void activate() {
FirebaseAnalytics.getInstance(mContext).setAnalyticsCollectionEnabled(true);
}
@Override
public void deactivate() {
FirebaseAnalytics.getInstance(mContext).setAnalyticsCollectionEnabled(false);
}
@Override
public void setUserId(String userId) {
FirebaseAnalytics.getInstance(mContext).setUserId(userId);
}
@Override
public Map<String, String> transformParameters(Map<String, String> parameters) {
return parameters;
}
@Override
public void logEvent(@NonNull String eventName, Map<String, String> transformedParams) {
Bundle bundle = buildEventBundle(transformedParams);
FirebaseAnalytics.getInstance(mContext).logEvent(eventName, bundle);
}
private Bundle buildEventBundle(Map<String, String> parameters) {
Bundle params = new Bundle();
for (Map.Entry<String, String> parameter : parameters.entrySet()) {
String key = parameter.getKey();
String value = parameter.getValue();
params.putString(key, value);
}
return params;
}
}
import android.content.Context
import android.os.Bundle
import com.copilot.analytics.EventLogProvider
import com.google.firebase.analytics.FirebaseAnalytics
class FirebaseAnalyticsEventLogProvider(context: Context) : EventLogProvider {
private val context: Context = context.applicationContext
companion object {
private val TAG = FirebaseAnalyticsEventLogProvider::class.java.simpleName
}
init {
deactivate()
}
override fun getProviderName(): String = TAG
override fun activate() {
FirebaseAnalytics.getInstance(context).setAnalyticsCollectionEnabled(true)
}
override fun deactivate() {
FirebaseAnalytics.getInstance(context).setAnalyticsCollectionEnabled(false)
}
override fun setUserId(userId: String?) {
FirebaseAnalytics.getInstance(context).setUserId(userId)
}
override fun transformParameters(parameters: Map<String, String>) = parameters
override fun logEvent(eventName: String, transformedParams: Map<String, String>?) {
val bundle = buildEventBundle(transformedParams)
FirebaseAnalytics.getInstance(context).logEvent(eventName, bundle)
}
private fun buildEventBundle(parameters: Map<String, String>?) = Bundle().apply {
parameters?.forEach { (k, v) -> putString(k, v) }
}
}
- Initialize the Copilot.cx SDK in your
Application
class with theApplicationContext
and a list ofLogProviders
as shown below (please refer the Managing providers section).
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
List<EventLogProvider> providers = new ArrayList<>();
providers.add(new FirebaseAnalyticsEventLogProvider(this));
Copilot.setup(this, providers)
.enableDebugLogs();
}
}
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
Copilot.setup(this, listOf(FirebaseAnalyticsEventLogProvider(this)))
.enableDebugLogs()
}
}
💡 You may add other
EventLogProvider
per your need. Every event will be dispatched to every provider configured in the system. It is suggested to add a console log only event provider for event dispatching debugging purposes.
Copilot logs are disabled by default. To enable them, add theenableDebugLogs()
command as shown in the example, Or by calling the function through the Copilot object:Copilot.getInstance().enableDebugLogs()
- Add Google Firebase to your Android project following this guide : Add firebase to your Android Project. Make sure you will end up with
google-services.json
file in your project.