Skip to content

Commit

Permalink
Merge pull request #33 from aminekarimii/feature/amplitude-addon
Browse files Browse the repository at this point in the history
Create amplitude addon
  • Loading branch information
aminekarimii committed Jun 21, 2023
2 parents 677c278 + cd418e9 commit 393faf2
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 5 deletions.
1 change: 1 addition & 0 deletions addon/analytiks-amplitude/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
35 changes: 35 additions & 0 deletions addon/analytiks-amplitude/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
}

android {
namespace 'com.analytiks.addon.amplitude'
compileSdk 33

defaultConfig {
minSdk 21
targetSdk 33

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}

dependencies {
implementation project(':analytiks-core')
implementation 'com.amplitude:analytics-android:1.8.2'
}
21 changes: 21 additions & 0 deletions addon/analytiks-amplitude/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
7 changes: 7 additions & 0 deletions addon/analytiks-amplitude/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.analytiks.addon.amplitude

import android.content.Context
import com.amplitude.android.Amplitude
import com.amplitude.android.Configuration
import com.amplitude.android.events.Identify
import com.amplitude.common.Logger
import com.analytiks.core.AnalyticsDataTransmitterExtension
import com.analytiks.core.CoreAddon
import com.analytiks.core.EventsExtension
import com.analytiks.core.UserProfileExtension
import com.analytiks.core.formatters.MapFormatStrategy
import com.analytiks.core.model.Param
import com.analytiks.core.model.UserProperty

class AmplitudeClient(
private val token: String,
private val serverGeoZone: ServerGeoZone = ServerGeoZone.EU,
private val optOut: Boolean = false,
private val minTimeBetweenSessionsMillis: Long = 10000
) : CoreAddon, EventsExtension, UserProfileExtension, AnalyticsDataTransmitterExtension {
private lateinit var amplitude: Amplitude
private val mapStrategy by lazy {
MapFormatStrategy()
}

override fun initialize(context: Context) {
amplitude = Amplitude(
Configuration(
apiKey = token,
context = context,
serverZone = serverGeoZone.serverZone,
optOut = optOut,
minTimeBetweenSessionsMillis = minTimeBetweenSessionsMillis
)
)

amplitude.logger.logMode = Logger.LogMode.DEBUG
}

override fun reset() {
amplitude.reset()
}

override fun logEvent(name: String) {
amplitude.track(name)
}

override fun logEvent(name: String, vararg properties: Param) {
amplitude.track(name, mapStrategy(*properties))
}

override fun identify(userId: String) {
amplitude.setUserId(userId)
}

override fun setUserProperty(property: UserProperty) {
val identify = Identify()
property.propertyValue?.let {
identify.set(
property.propertyName,
it
)
}
amplitude.identify(identify)
}

override fun setUserPropertyOnce(property: UserProperty) = Unit
override fun pushAll() {
amplitude.flush()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.analytiks.addon.amplitude

import com.amplitude.core.ServerZone

enum class ServerGeoZone(val serverZone: ServerZone) {
US(ServerZone.US),
EU(ServerZone.EU)
}
4 changes: 1 addition & 3 deletions analytiks-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ android {

dependencies {

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.7.0'
api 'androidx.core:core-ktx:1.7.0'

testImplementation 'junit:junit:4.13.2'
testImplementation 'org.json:json:20201115'
Expand Down
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ dependencies {
implementation project(path: ':addon:analytiks-segment')
implementation project(path: ':addon:analytiks-timber')
implementation project(path: ':addon:analytiks-googleanalytics')
implementation project(path: ':addon:analytiks-amplitude')

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/java/com/logitanalyticsapp/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.logitanalyticsapp
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.analytiks.Analytiks
import com.analytiks.addon.amplitude.AmplitudeClient
import com.analytiks.addon.amplitude.ServerGeoZone
import com.analytiks.addon.googleanalytics.GoogleAnalyticsClient
import com.analytiks.addon.mixpanel.MixpanelAnalyticsClient
import com.analytiks.addon.timber.TimberLocalClient
Expand Down Expand Up @@ -35,7 +37,12 @@ class MainActivity : AppCompatActivity() {
flushIntervalInSeconds = 5,
trackApplicationLifecycleEvents = true,
),
GoogleAnalyticsClient(isAnalyticsCollectionEnabled = true)
GoogleAnalyticsClient(isAnalyticsCollectionEnabled = true),
AmplitudeClient(
token = "YOUR_TOKEN",
// It is recommended to check your server zone and set it.
serverGeoZone = ServerGeoZone.US
)
)

analytiks = Analytiks(clients)
Expand Down
3 changes: 2 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ include ':addon:analytiks-azureinsight'
include ':addon:analytiks-googleanalytics'
include ':addon:analytiks-timber'
include ':analytiks-core'
include ':addon:analytiks-segment'
include ':addon:analytiks-segment'
include ':addon:analytiks-amplitude'

0 comments on commit 393faf2

Please sign in to comment.