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.MANAGE_TYPE"
android:value="YourOwn"/>
Replace
COPILOT_ENVIRONMENT_URL_HERE
with the appropriate value (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();
}
@Override
public String getProviderName() {
return TAG;
}
@Override
public void activate() {
// No implementation
}
@Override
public void deactivate() {
// No implementation
}
@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 {
companion object {
private val TAG = FirebaseAnalyticsEventLogProvider::class.java.simpleName
}
private val mContext: Context = context.applicationContext
override fun getProviderName(): String = TAG
override fun activate() {
// No implementation
}
override fun deactivate() {
// No implementation
}
override fun setUserId(userId: String?) {
FirebaseAnalytics.getInstance(mContext).setUserId(userId)
}
override fun transformParameters(parameters: Map<String, String>): Map<String, String> = parameters
override fun logEvent(eventName: String, transformedParams: Map<String, String>) {
val bundle = buildEventBundle(transformedParams)
FirebaseAnalytics.getInstance(mContext).logEvent(eventName, bundle)
}
private fun buildEventBundle(parameters: Map<String, String>): Bundle {
val params = Bundle()
for ((key, value) in parameters) {
params.putString(key, value)
}
return params
}
}
- Initialize the Copilot.cx SDK in your
Application
class with theApplicationContext
and the listEventLogProvider
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()
val providers: MutableList<EventLogProvider> = ArrayList()
providers.add(FirebaseAnalyticsEventLogProvider(this))
Copilot.setup(this, providers)
.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.