Skip to content

Commit 4a5d2be

Browse files
authored
Feature/add full deep link support to kotlin sample (#133)
* Adding full support for deep-links in Android Kotlin. * Remove unnecessary function. * Change function to be non-static and the name to be more inline with the rest of the functions.
1 parent fc5d0a9 commit 4a5d2be

File tree

5 files changed

+75
-20
lines changed

5 files changed

+75
-20
lines changed

android/src/main/java/com/segment/analytics/kotlin/android/AndroidAnalytics.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.segment.analytics.kotlin.android
22

33
import android.content.Context
4+
import android.content.Intent
45
import android.util.Log
56
import com.segment.analytics.kotlin.android.plugins.AndroidContextPlugin
67
import com.segment.analytics.kotlin.android.plugins.AndroidLifecyclePlugin
8+
import com.segment.analytics.kotlin.android.utilities.DeepLinkUtils
79
import com.segment.analytics.kotlin.core.Analytics
810
import com.segment.analytics.kotlin.core.Configuration
911
import com.segment.analytics.kotlin.core.platform.plugins.logger.*
@@ -68,6 +70,20 @@ private fun Analytics.startup() {
6870
remove(targetType = ConsoleTarget::class)
6971
}
7072

73+
/**
74+
* Track a deep link manually.
75+
*
76+
* This function is meant to be called by the user in places were we need to manually track a
77+
* deep link opened event. For example, in the cause of an Activity being sent a new intent in it's
78+
* onNewIntent() function. The URI that triggered the intent will be in the Intent.data property.
79+
*
80+
* @param referrer: The string representing the app or url that caused the deep link to be activated.
81+
* @param intent: The intent received by the Activity's onNewIntent() function.
82+
*/
83+
fun Analytics.trackDeepLinkOpen(referrer: String?, intent: Intent?) {
84+
DeepLinkUtils(this).trackDeepLinkFrom(referrer, intent)
85+
}
86+
7187
class AndroidLogTarget: LogTarget {
7288
override fun parseLog(log: LogMessage) {
7389
var metadata = ""

android/src/main/java/com/segment/analytics/kotlin/android/plugins/AndroidLifecyclePlugin.kt

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import androidx.lifecycle.Lifecycle
1414
import androidx.lifecycle.LifecycleObserver
1515
import androidx.lifecycle.LifecycleOwner
1616
import androidx.lifecycle.ProcessLifecycleOwner
17+
import com.segment.analytics.kotlin.android.utilities.DeepLinkUtils
1718
import com.segment.analytics.kotlin.core.Analytics
1819
import com.segment.analytics.kotlin.core.Storage
1920
import com.segment.analytics.kotlin.core.platform.Plugin
@@ -211,27 +212,11 @@ class AndroidLifecyclePlugin() : Application.ActivityLifecycleCallbacks, Default
211212

212213
private fun trackDeepLink(activity: Activity?) {
213214
val intent = activity?.intent
214-
if (intent == null || intent.data == null) {
215-
return
216-
}
217-
val properties = buildJsonObject {
218-
219-
getReferrer(activity)?.let {
220-
put("referrer", it.toString())
221-
}
222215

223-
val uri = intent.data
224-
uri?.let {
225-
for (parameter in uri.queryParameterNames) {
226-
val value = uri.getQueryParameter(parameter)
227-
if (value != null && value.trim().isNotEmpty()) {
228-
put(parameter, value)
229-
}
230-
}
231-
put("url", uri.toString())
232-
}
216+
intent?.let {
217+
val referrer = getReferrer(activity)?.toString()
218+
DeepLinkUtils(analytics).trackDeepLinkFrom(referrer, it)
233219
}
234-
analytics.track("Deep Link Opened", properties)
235220
}
236221

237222
internal fun trackApplicationLifecycleEvents() {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.segment.analytics.kotlin.android.utilities
2+
3+
import android.content.Intent
4+
import com.segment.analytics.kotlin.core.Analytics
5+
import kotlinx.serialization.json.buildJsonObject
6+
import kotlinx.serialization.json.put
7+
8+
class DeepLinkUtils(val analytics: Analytics) {
9+
10+
fun trackDeepLinkFrom(referrer: String?, intent: Intent?) {
11+
12+
if (intent == null || intent.data == null) {
13+
return
14+
}
15+
16+
val properties = buildJsonObject {
17+
18+
referrer?.let {
19+
put("referrer", it)
20+
}
21+
22+
val uri = intent.data
23+
uri?.let {
24+
for (parameter in uri.queryParameterNames) {
25+
val value = uri.getQueryParameter(parameter)
26+
if (value != null && value.trim().isNotEmpty()) {
27+
put(parameter, value)
28+
}
29+
}
30+
put("url", uri.toString())
31+
}
32+
}
33+
analytics.track("Deep Link Opened", properties)
34+
}
35+
}

samples/kotlin-android-app/src/main/AndroidManifest.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
android:name="com.google.android.gms.ads.APPLICATION_ID"
2727
android:value="ca-app-pub-3940256099942544~3347511713" />
2828
<activity android:name=".MainActivity"
29+
android:launchMode="singleInstance"
2930
android:exported="true">
3031
<intent-filter>
3132
<action android:name="android.intent.action.MAIN" />
@@ -38,9 +39,12 @@
3839
<category android:name="android.intent.category.DEFAULT" />
3940
<category android:name="android.intent.category.BROWSABLE" />
4041

42+
<!-- Trigger Deep-link from cmd line:
43+
adb shell am start -a android.intent.action.VIEW -c android.intent.category.DEFAULT -d "seg://segment-sample.com"
44+
-->
4145
<data
4246
android:host="segment-sample.com"
43-
android:scheme="https" />
47+
android:scheme="seg" />
4448
</intent-filter>
4549
</activity>
4650
<service

samples/kotlin-android-app/src/main/java/com/segment/analytics/next/MainActivity.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.segment.analytics.next
22

3+
import android.content.Intent
34
import android.os.Bundle
45
import android.widget.Button
56
import androidx.appcompat.app.AppCompatActivity
67
import androidx.fragment.app.Fragment
78
import androidx.fragment.app.FragmentManager
89
import androidx.fragment.app.FragmentTransaction
10+
import com.segment.analytics.kotlin.android.trackDeepLinkOpen
911
import com.segment.analytics.kotlin.core.Analytics
1012
import com.segment.analytics.kotlin.core.EventType
1113
import com.segment.analytics.kotlin.core.platform.plugins.logger.log
@@ -52,4 +54,17 @@ class MainActivity : AppCompatActivity() {
5254
fragmentTransaction.replace(R.id.frameLayout, fragment)
5355
fragmentTransaction.commit() // save the changes
5456
}
57+
58+
override fun onNewIntent(intent: Intent?) {
59+
super.onNewIntent(intent)
60+
61+
// Add a deep-link opened event manually.
62+
// This is necessary when your Activity has a android:launchMode of
63+
// 'singleInstance', 'singleInstancePerTask', 'singleTop', or any other mode
64+
// that will re-use an existing Activity instead of creating a new instance.
65+
// The Analytics SDK automatically identifies when you app is started from a deep link
66+
// if the Activity is created, but not if it is re-used. Therefore we have to add this
67+
// code to manually capture the Deep Link info.
68+
analytics.trackDeepLinkOpen("deep-link", intent)
69+
}
5570
}

0 commit comments

Comments
 (0)