Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deeplink not getting tracked. #127

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ import java.util.*
val WIDEVINE_UUID = UUID(-0x121074568629b532L, -0x5c37d8232ae2de13L)

/** AnalyticsPlugin */
class AnalyticsPlugin : FlutterPlugin, NativeContextApi, EventChannel.StreamHandler, ActivityAware,
class AnalyticsPlugin : FlutterPlugin, NativeContextApi, EventChannel.StreamHandler, ActivityAware,
PluginRegistry.NewIntentListener {
private var context: Context? = null

private val pendingDeeplinkEventsQueue: Queue<Intent> = LinkedList()
private fun ByteArray.toHexString() = joinToString("") { "%02x".format(it) }

private val eventsChannel = "analytics/deep_link_events"
Expand Down Expand Up @@ -182,13 +182,13 @@ class AnalyticsPlugin : FlutterPlugin, NativeContextApi, EventChannel.StreamHand
private fun createChangeReceiver(events: EventSink): BroadcastReceiver {
return object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent) {
val referringApplication = intent.getStringExtra("referringApplication")
val referringApplication = intent.getStringExtra("referring_application")
// NOTE: assuming intent.getAction() is Intent.ACTION_VIEW
val dataString: String? = intent.dataString
if (dataString == null) {
events.error("UNAVAILABLE", "Link unavailable", null)
} else {
val data = mapOf("url" to dataString, "referringApplication" to referringApplication)
val data = mapOf("url" to dataString, "referring_application" to referringApplication)
events.success(data)
}
}
Expand All @@ -198,13 +198,30 @@ class AnalyticsPlugin : FlutterPlugin, NativeContextApi, EventChannel.StreamHand
private fun handleIntent(context: Context, intent: Intent) {
val action = intent.action
if (Intent.ACTION_VIEW == action) {
if (changeReceiver != null) changeReceiver!!.onReceive(context, intent)
if (changeReceiver != null) {
changeReceiver!!.onReceive(context, intent)
} else {
pendingDeeplinkEventsQueue.add(intent.cloneFilter())
}
}
}

private fun processPendingDeeplinkEventsQueue() {
if (this.context == null ||
changeReceiver == null
) {
return
}
while (pendingDeeplinkEventsQueue.isNotEmpty()) {
val intent = pendingDeeplinkEventsQueue.poll()
changeReceiver!!.onReceive(context, intent)
}
}

override fun onListen(arguments: Any?, events: EventSink?) {
if (events != null) {
this.changeReceiver = createChangeReceiver(events)
processPendingDeeplinkEventsQueue()
}
}

Expand All @@ -216,7 +233,10 @@ class AnalyticsPlugin : FlutterPlugin, NativeContextApi, EventChannel.StreamHand
binding.addOnNewIntentListener(this)
if (this.context != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
binding.activity.intent.putExtra("referringApplication", binding.activity.referrer.toString())
Copy link

Choose a reason for hiding this comment

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

this key should be "referrer" according to segment docs I believe.

Choose a reason for hiding this comment

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

I can confirm it's "referrer" in analytics-kotlin.

binding.activity.intent.putExtra(
"referring_application",
binding.activity.referrer.toString()
)
}
this.handleIntent(this.context!!, binding.activity.intent)
}
Expand Down
30 changes: 21 additions & 9 deletions packages/core/ios/Classes/AnalyticsPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import UIKit
import Foundation

public class AnalyticsPlugin: NSObject, FlutterPlugin, NativeContextApi, FlutterStreamHandler, FlutterApplicationLifeCycleDelegate {
private var pendingDeeplinkEventsQueue:[[String:String?]] = []
public func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
_eventSink = events
processPendingDeeplinkEventsQueue();
return nil
}

Expand All @@ -14,19 +16,27 @@ public class AnalyticsPlugin: NSObject, FlutterPlugin, NativeContextApi, Flutter
}

var _eventSink:FlutterEventSink?;
public func application(_ application: UIApplication, open url: URL, sourceApplication: String, annotation: Any) -> Bool {
public func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool{
let sourceApplication = options[.sourceApplication] as? String;
if (_eventSink != nil) {
_eventSink?(["url": url.absoluteString, "referringApplication": sourceApplication])
_eventSink?(["url": url.absoluteString, "referring_application": sourceApplication])
}else{
pendingDeeplinkEventsQueue.append(["url": url.absoluteString, "referring_application": sourceApplication]);
}
return true


return false
}
public func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
if (_eventSink != nil) {
_eventSink?([url: url.absoluteString])

private func processPendingDeeplinkEventsQueue() -> Void{
if(_eventSink == nil){
return;
}
while(!pendingDeeplinkEventsQueue.isEmpty){
let eventData:[String:String?] = pendingDeeplinkEventsQueue.removeFirst();
_eventSink?(eventData);
Rjaintwilio marked this conversation as resolved.
Show resolved Hide resolved
}
return true
}

internal static var device = VendorSystem.current

func getContext(collectDeviceId: Bool, completion: @escaping (Result<NativeContext, Error>) -> Void) {
Expand Down Expand Up @@ -88,10 +98,12 @@ public class AnalyticsPlugin: NSObject, FlutterPlugin, NativeContextApi, Flutter

public static func register(with registrar: FlutterPluginRegistrar) {
let messenger : FlutterBinaryMessenger = registrar.messenger()
let api : NativeContextApi & NSObjectProtocol & AnalyticsPlugin = AnalyticsPlugin.init()
let plugin = AnalyticsPlugin.init();
let api : NativeContextApi & NSObjectProtocol & AnalyticsPlugin = plugin
NativeContextApiSetup.setUp(binaryMessenger: messenger, api: api)

let channel:FlutterEventChannel = FlutterEventChannel(name: "analytics/deep_link_events", binaryMessenger: registrar.messenger())
channel.setStreamHandler(api)
registrar.addApplicationDelegate(plugin)
}
}
2 changes: 1 addition & 1 deletion packages/core/lib/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ class DeepLinkDataState extends PersistedState<DeepLinkData> {

@JsonSerializable(explicitToJson: true, includeIfNull: false)
class DeepLinkData {
final String referringApplication;
final String? referringApplication;
final String url;

DeepLinkData(this.referringApplication, this.url);
Expand Down
20 changes: 14 additions & 6 deletions packages/core/lib/state.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading