Skip to content

Commit 58e0284

Browse files
committed
Preliminary changes to add referrer to context. Ideally the deeplink event would set the url on the userInfo, but currently that isn't accessible from the native layers where deeplinks are handled. (i.e. referrer would be reset with userinfo).
1 parent d102dc0 commit 58e0284

File tree

12 files changed

+47
-15
lines changed

12 files changed

+47
-15
lines changed

packages/core/android/src/main/kotlin/com/segment/analytics/AnalyticsPlugin.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class AnalyticsPlugin : FlutterPlugin, NativeContextApi, EventChannel.StreamHand
3838
private fun ByteArray.toHexString() = joinToString("") { "%02x".format(it) }
3939

4040
private val eventsChannel = "analytics/deep_link_events"
41+
private val referrerUrl: String? = null
4142

4243
override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
4344
context = flutterPluginBinding.applicationContext
@@ -166,6 +167,7 @@ class AnalyticsPlugin : FlutterPlugin, NativeContextApi, EventChannel.StreamHand
166167
name = "Android",
167168
version = Build.VERSION.RELEASE,
168169
),
170+
referrer = referrerUrl,
169171
screen = NativeContextScreen(
170172
height = displayMetrics.heightPixels.toLong(),
171173
width = displayMetrics.widthPixels.toLong(),
@@ -190,6 +192,7 @@ class AnalyticsPlugin : FlutterPlugin, NativeContextApi, EventChannel.StreamHand
190192
} else {
191193
val data = mapOf("url" to dataString, "referring_application" to referringApplication)
192194
events.success(data)
195+
referrerUrl = dataString
193196
}
194197
}
195198
}

packages/core/android/src/main/kotlin/com/segment/analytics/Context.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ data class NativeContext (
3030
val locale: String? = null,
3131
val network: NativeContextNetwork? = null,
3232
val os: NativeContextOS? = null,
33+
val referrer: String? = null,
3334
val screen: NativeContextScreen? = null,
3435
val timezone: String? = null,
3536
val userAgent: String? = null
@@ -54,12 +55,13 @@ data class NativeContext (
5455
val os: NativeContextOS? = (list[5] as? List<Any?>)?.let {
5556
NativeContextOS.fromList(it)
5657
}
57-
val screen: NativeContextScreen? = (list[6] as? List<Any?>)?.let {
58+
val referrer = list[6] as? String
59+
val screen: NativeContextScreen? = (list[7] as? List<Any?>)?.let {
5860
NativeContextScreen.fromList(it)
5961
}
60-
val timezone = list[7] as? String
61-
val userAgent = list[8] as? String
62-
return NativeContext(app, device, library, locale, network, os, screen, timezone, userAgent)
62+
val timezone = list[8] as? String
63+
val userAgent = list[9] as? String
64+
return NativeContext(app, device, library, locale, network, os, referrer, screen, timezone, userAgent)
6365
}
6466
}
6567
fun toList(): List<Any?> {
@@ -70,6 +72,7 @@ data class NativeContext (
7072
locale,
7173
network?.toList(),
7274
os?.toList(),
75+
referrer,
7376
screen?.toList(),
7477
timezone,
7578
userAgent,

packages/core/ios/Classes/AnalyticsPlugin.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Foundation
44

55
public class AnalyticsPlugin: NSObject, FlutterPlugin, NativeContextApi, FlutterStreamHandler, FlutterApplicationLifeCycleDelegate {
66
private var pendingDeeplinkEventsQueue:[[String:String?]] = []
7+
private var referrerUrl: String? = nil
78
public func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? {
89
_eventSink = events
910
processPendingDeeplinkEventsQueue();
@@ -89,6 +90,7 @@ public class AnalyticsPlugin: NSObject, FlutterPlugin, NativeContextApi, Flutter
8990
os: NativeContextOS(
9091
name: device.systemName,
9192
version: device.systemVersion),
93+
referrer: referrerUrl,
9294
screen: NativeContextScreen(
9395
height: Int32(screen.height),
9496
width: Int32(screen.width)),

packages/core/ios/Classes/Context.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ struct NativeContext {
3939
var locale: String? = nil
4040
var network: NativeContextNetwork? = nil
4141
var os: NativeContextOS? = nil
42+
var referrer: String? = nil
4243
var screen: NativeContextScreen? = nil
4344
var timezone: String? = nil
4445
var userAgent: String? = nil
@@ -65,12 +66,13 @@ struct NativeContext {
6566
if let osList = list[5] as? [Any?] {
6667
os = NativeContextOS.fromList(osList)
6768
}
69+
var referrer: String? = list[6] as? String
6870
var screen: NativeContextScreen? = nil
69-
if let screenList = list[6] as? [Any?] {
71+
if let screenList = list[7] as? [Any?] {
7072
screen = NativeContextScreen.fromList(screenList)
7173
}
72-
let timezone = list[7] as? String
73-
let userAgent = list[8] as? String
74+
let timezone = list[8] as? String
75+
let userAgent = list[9] as? String
7476

7577
return NativeContext(
7678
app: app,
@@ -79,6 +81,7 @@ struct NativeContext {
7981
locale: locale,
8082
network: network,
8183
os: os,
84+
referrer: referrer,
8285
screen: screen,
8386
timezone: timezone,
8487
userAgent: userAgent
@@ -92,6 +95,7 @@ struct NativeContext {
9295
locale,
9396
network?.toList(),
9497
os?.toList(),
98+
referrer,
9599
screen?.toList(),
96100
timezone,
97101
userAgent,

packages/core/lib/analytics_web.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class AnalyticsPlatformImpl extends AnalyticsPlatform {
2323
),
2424
userAgent: web.window.navigator.userAgent,
2525
locale: web.window.navigator.language,
26+
referrer: web.window.document.referrer, // SETH PLZ CHECK ME ON THIS
2627
screen: NativeContextScreen(
2728
height: web.window.screen.height,
2829
width: web.window.screen.width,

packages/core/lib/event.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,14 @@ class Context extends JSONExtendableImpl {
327327
String locale;
328328
ContextNetwork network;
329329
ContextOS os;
330+
String referrer;
330331
ContextScreen screen;
331332
String timezone;
332333
String? instanceId;
333334
UserTraits traits;
334335

335336
Context(this.app, this.device, this.library, this.locale, this.network,
336-
this.os, this.screen, this.timezone, this.traits,
337+
this.os, this.referrer, this.screen, this.timezone, this.traits,
337338
{this.instanceId, super.custom});
338339
Context.fromNative(NativeContext nativeContext, this.traits)
339340
: app = nativeContext.app == null
@@ -355,6 +356,7 @@ class Context extends JSONExtendableImpl {
355356
os = nativeContext.os == null
356357
? ContextOS("", "")
357358
: ContextOS.fromNative(nativeContext.os as NativeContextOS),
359+
referrer = nativeContext.referrer ?? "",
358360
screen = nativeContext.screen == null
359361
? ContextScreen(0, 0)
360362
: ContextScreen.fromNative(
@@ -372,6 +374,7 @@ class Context extends JSONExtendableImpl {
372374
"locale",
373375
"networm",
374376
"os",
377+
"referrer",
375378
"screen",
376379
"timezone",
377380
"traits"
@@ -651,6 +654,7 @@ Context mergeContext(Context a, Context b) {
651654
a.locale,
652655
a.network,
653656
a.os,
657+
a.referrer,
654658
mergeContextScreen(a.screen, b.screen),
655659
a.timezone,
656660
a.traits,

packages/core/lib/event.g.dart

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/core/lib/native_context.dart

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class NativeContext {
1616
this.locale,
1717
this.network,
1818
this.os,
19+
this.referrer,
1920
this.screen,
2021
this.timezone,
2122
this.userAgent,
@@ -33,6 +34,8 @@ class NativeContext {
3334

3435
NativeContextOS? os;
3536

37+
String? referrer;
38+
3639
NativeContextScreen? screen;
3740

3841
String? timezone;
@@ -47,6 +50,7 @@ class NativeContext {
4750
locale,
4851
network?.encode(),
4952
os?.encode(),
53+
referrer,
5054
screen?.encode(),
5155
timezone,
5256
userAgent,
@@ -72,11 +76,12 @@ class NativeContext {
7276
os: result[5] != null
7377
? NativeContextOS.decode(result[5]! as List<Object?>)
7478
: null,
75-
screen: result[6] != null
76-
? NativeContextScreen.decode(result[6]! as List<Object?>)
79+
referrer: result[6] as String?,
80+
screen: result[7] != null
81+
? NativeContextScreen.decode(result[7]! as List<Object?>)
7782
: null,
78-
timezone: result[7] as String?,
79-
userAgent: result[8] as String?,
83+
timezone: result[8] as String?,
84+
userAgent: result[9] as String?,
8085
);
8186
}
8287
}

packages/core/macos/Classes/AnalyticsPlugin.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ internal static var device = VendorSystem.current
5454
os: NativeContextOS(
5555
name: device.systemName,
5656
version: device.systemVersion),
57+
referrer: nil,
5758
screen: NativeContextScreen(
5859
height: Int32(screen.height),
5960
width: Int32(screen.width)),

packages/core/macos/Classes/Context.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ struct NativeContext {
3939
var locale: String? = nil
4040
var network: NativeContextNetwork? = nil
4141
var os: NativeContextOS? = nil
42+
var referrer: String? = nil
4243
var screen: NativeContextScreen? = nil
4344
var timezone: String? = nil
4445
var userAgent: String? = nil
@@ -65,12 +66,13 @@ struct NativeContext {
6566
if let osList = list[5] as? [Any?] {
6667
os = NativeContextOS.fromList(osList)
6768
}
69+
let referrer = list[6] as? String
6870
var screen: NativeContextScreen? = nil
69-
if let screenList = list[6] as? [Any?] {
71+
if let screenList = list[7] as? [Any?] {
7072
screen = NativeContextScreen.fromList(screenList)
7173
}
72-
let timezone = list[7] as? String
73-
let userAgent = list[8] as? String
74+
let timezone = list[8] as? String
75+
let userAgent = list[9] as? String
7476

7577
return NativeContext(
7678
app: app,
@@ -79,6 +81,7 @@ struct NativeContext {
7981
locale: locale,
8082
network: network,
8183
os: os,
84+
referrer: referrer,
8285
screen: screen,
8386
timezone: timezone,
8487
userAgent: userAgent
@@ -92,6 +95,7 @@ struct NativeContext {
9295
locale,
9396
network?.toList(),
9497
os?.toList(),
98+
referrer,
9599
screen?.toList(),
96100
timezone,
97101
userAgent,

0 commit comments

Comments
 (0)