Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion samples/kotlin-android-app-destinations/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ android {
defaultConfig {
multiDexEnabled true
applicationId "com.segment.analytics.destinations"
minSdkVersion 16
minSdkVersion 21
targetSdkVersion 30
versionCode 3
versionName "2.0"
Expand All @@ -34,6 +34,11 @@ android {
kotlinOptions {
jvmTarget = '1.8'
}
testOptions {
unitTests.all {
useJUnitPlatform()
}
}
}

dependencies {
Expand All @@ -47,6 +52,10 @@ dependencies {
// When using the BoM, you don't specify versions in Firebase library dependencies
implementation 'com.google.firebase:firebase-analytics-ktx'

// Intercom
implementation 'io.intercom.android:intercom-sdk-base:10.1.1'
implementation 'io.intercom.android:intercom-sdk-fcm:10.1.1'

implementation project(':android')

implementation 'androidx.multidex:multidex:2.0.1'
Expand All @@ -63,7 +72,11 @@ dependencies {
implementation 'androidx.lifecycle:lifecycle-process:2.3.1'
implementation 'androidx.lifecycle:lifecycle-common-java8:2.3.1'

testImplementation 'io.mockk:mockk:1.10.6'
testImplementation 'junit:junit:4.+'
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.2'
testImplementation platform("org.junit:junit-bom:5.7.2")
testImplementation "org.junit.jupiter:junit-jupiter"
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.segment.analytics.destinations

import android.app.Application
import com.segment.analytics.destinations.plugins.AmplitudeSession
import com.segment.analytics.destinations.plugins.FirebaseDestination
import com.segment.analytics.destinations.plugins.MixpanelDestination
import com.segment.analytics.destinations.plugins.WebhookPlugin
import com.segment.analytics.destinations.plugins.*
import com.segment.analytics.kotlin.android.Analytics
import com.segment.analytics.kotlin.core.Analytics
import java.util.concurrent.Executors
Expand Down Expand Up @@ -40,5 +37,8 @@ class MainApplication : Application() {

// Try out Firebase Destination
analytics.add(FirebaseDestination(applicationContext))

// Try out Intercom destination
analytics.add(IntercomDestination(this))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
package com.segment.analytics.destinations.plugins

import android.app.Application
import com.segment.analytics.kotlin.android.plugins.AndroidLifecycle
import com.segment.analytics.kotlin.core.*
import com.segment.analytics.kotlin.core.platform.DestinationPlugin
import com.segment.analytics.kotlin.core.platform.Plugin
import com.segment.analytics.kotlin.core.platform.plugins.log
import com.segment.analytics.kotlin.core.utilities.*
import io.intercom.android.sdk.Company
import io.intercom.android.sdk.Intercom
import io.intercom.android.sdk.UserAttributes
import io.intercom.android.sdk.identity.Registration
import kotlinx.serialization.json.*

/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

This is an example of the Intercom device-mode destination plugin that can be integrated with
Segment analytics.
Note: This plugin is NOT SUPPORTED by Segment. It is here merely as an example,
and for your convenience should you find it useful.
# Instructions for adding Intercom:
- In your app-module build.gradle file add the following:
```
...
dependencies {
...
// Intercom
implementation 'io.intercom.android:intercom-sdk-base:10.1.1'
implementation 'io.intercom.android:intercom-sdk-fcm:10.1.1'
}
```
- Copy this entire IntercomDestination.kt file into your project's codebase.
- Go to your project's codebase and wherever u initialize the analytics client add these lines
```
val intercom = IntercomDestination()
analytics.add(intercom)
```

Note: due to the inclusion of Intercom partner integration your minSdk cannot be smaller than 21

MIT License
Copyright (c) 2021 Segment
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

class IntercomDestination(
private val application: Application
): DestinationPlugin(), AndroidLifecycle {

override val key: String = "Intercom"
private var mobileApiKey: String = ""
private var appId: String = ""
lateinit var intercom: Intercom
private set

override fun update(settings: Settings, type: Plugin.UpdateType) {
// if we've already set up this singleton SDK, can't do it again, so skip.
if (type != Plugin.UpdateType.Initial) return
super.update(settings, type)

settings.integrations[key]?.jsonObject?.let {
mobileApiKey = it.getString("mobileApiKey") ?: ""
appId = it.getString("appId") ?: ""
}

Intercom.initialize(application, mobileApiKey, appId)
this.intercom = Intercom.client()
}

override fun track(payload: TrackEvent): BaseEvent? {
val result = super.track(payload)

val eventName = payload.event
val properties = payload.properties

if (!properties.isNullOrEmpty()) {
val price = buildJsonObject{
val amount = properties.getDouble(REVENUE) ?: properties.getDouble(TOTAL)
amount?.let {
put(AMOUNT, it * 100)
}

properties.getString(CURRENCY)?.let {
put(CURRENCY, it)
}
}

val eventProperties = buildJsonObject {
if (!price.isNullOrEmpty()) {
put(PRICE, price)
}

properties.forEach { (key, value) ->
// here we are only interested in primitive values and not maps or collections
if (key !in setOf("products", REVENUE, TOTAL, CURRENCY)
&& value is JsonPrimitive) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we checking the type here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a translation from the original java code, where it excludes all the map and list values. check these lines.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh i see. lets add a comment here stating that we are only interested in primitive values and not maps or collections

put(key, value)
}
}
}

intercom.logEvent(eventName, eventProperties)
analytics.log("Intercom.client().logEvent($eventName, $eventProperties)")
}
else {
intercom.logEvent(eventName)
analytics.log("Intercom.client().logEvent($eventName)")
}

return result
}

override fun identify(payload: IdentifyEvent): BaseEvent? {
val result = super.identify(payload)

val userId = payload.userId
if (userId.isEmpty()) {
intercom.registerUnidentifiedUser()
analytics.log("Intercom.client().registerUnidentifiedUser()")
}
else {
val registration = Registration.create().withUserId(userId)
intercom.registerIdentifiedUser(registration)
analytics.log("Intercom.client().registerIdentifiedUser(registration)")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might need to be
"Intercom.client().registerIdentifiedUser($registration)"
with the idea being that we are logging the value of registration

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah. this is something I wasn't sure of. the original java version logs it this way. I was wondering if registration has sensitive data, so the java version is doing it on purpose?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh interesting. lets keep it as is then for now

}

val intercomOptions = payload.integrations["Intercom"]?.safeJsonObject
intercomOptions?.getString("userHash")?.let {
intercom.setUserHash(it)
}

if (!payload.traits.isNullOrEmpty() && !intercomOptions.isNullOrEmpty()) {
setUserAttributes(payload.traits, intercomOptions)
}
else {
setUserAttributes(payload.traits, null)
}

return result
}

override fun group(payload: GroupEvent): BaseEvent? {
val result = super.group(payload)

if (payload.groupId.isNotEmpty()) {
val traits = buildJsonObject {
putAll(payload.traits)
put("id", payload.groupId)
}
val company = setCompany(traits)
val userAttributes = UserAttributes.Builder()
.withCompany(company)
.build()
intercom.updateUser(userAttributes)
}

return result
}

override fun reset() {
super.reset()
intercom.logout()
analytics.log("Intercom.client().reset()")
}

private fun setUserAttributes(traits: Traits, intercomOptions: JsonObject?) {
val builder = UserAttributes.Builder()

traits.getString(NAME)?.let { builder.withName(it) }
traits.getString(EMAIL)?.let { builder.withEmail(it) }
traits.getString(PHONE)?.let { builder.withPhone(it) }

intercomOptions?.let {
builder.withLanguageOverride(it.getString(LANGUAGE_OVERRIDE))
builder.withSignedUpAt(it.getLong(CREATED_AT))
builder.withUnsubscribedFromEmails(it.getBoolean(UNSUBSCRIBED_FROM_EMAILS))
}

traits[COMPANY]?.safeJsonObject?.let {
val company = setCompany(it)
builder.withCompany(company)
}

traits.forEach { (key, value) ->
// here we are only interested in primitive values and not maps or collections
if (value is JsonPrimitive &&
key !in setOf(NAME, EMAIL, PHONE, "userId", "anonymousId")) {
builder.withCustomAttribute(key, value.toContent())
}
}

intercom.updateUser(builder.build())
analytics.log("Intercom.client().updateUser(userAttributes)")
}

private fun setCompany(company: JsonObject): Company {
val builder = Company.Builder()
company.getString("id")?.let {
builder.withCompanyId(it)
} ?: return builder.build()

company.getString(NAME)?.let { builder.withName(it) }
company.getLong(CREATED_AT)?.let { builder.withCreatedAt(it) }
company.getInt(MONTHLY_SPEND)?.let { builder.withMonthlySpend(it) }
company.getString(PLAN)?.let { builder.withPlan(it) }

company.forEach { (key, value) ->
// here we are only interested in primitive values and not maps or collections
if (value is JsonPrimitive &&
key !in setOf("id", NAME, CREATED_AT, MONTHLY_SPEND, PLAN)
) {
builder.withCustomAttribute(key, value.toContent())
}
}

return builder.build()
}

companion object {

// Intercom common specced attributes
private const val NAME = "name"
private const val CREATED_AT = "createdAt"
private const val COMPANY = "company"
private const val PRICE = "price"
private const val AMOUNT = "amount"
private const val CURRENCY = "currency"

// Intercom specced user attributes
private const val EMAIL = "email"
private const val PHONE = "phone"
private const val LANGUAGE_OVERRIDE = "languageOverride"
private const val UNSUBSCRIBED_FROM_EMAILS = "unsubscribedFromEmails"

// Intercom specced group attributes
private const val MONTHLY_SPEND = "monthlySpend"
private const val PLAN = "plan"

// Segment specced properties
private const val REVENUE = "revenue"
private const val TOTAL = "total"
}
}
Loading