Skip to content

Commit 2956a05

Browse files
authored
feat: Add support for Get / Set SessionId on Rokt (#645)
* feat: Add support for Get / Set SessionId on Rokt * Fix lint and compilation issue
1 parent 2725387 commit 2956a05

7 files changed

Lines changed: 144 additions & 0 deletions

File tree

android-core/src/main/java/com/mparticle/internal/KitFrameworkWrapper.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,22 @@ public void prepareAttributesAsync(@NonNull Map<String, String> attributes) {
722722
}
723723
}
724724

725+
@Override
726+
public void setSessionId(@NonNull String sessionId) {
727+
if (mKitManager != null) {
728+
mKitManager.setSessionId(sessionId);
729+
}
730+
}
731+
732+
@Override
733+
@Nullable
734+
public String getSessionId() {
735+
if (mKitManager != null) {
736+
return mKitManager.getSessionId();
737+
}
738+
return null;
739+
}
740+
725741
static class CoreCallbacksImpl implements CoreCallbacks {
726742
KitFrameworkWrapper mKitFrameworkWrapper;
727743
ConfigManager mConfigManager;

android-core/src/main/java/com/mparticle/internal/KitManager.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,21 @@ void execute(@NonNull String identifier,
148148

149149
void close();
150150

151+
/**
152+
* Set the session id to use for the next execute call.
153+
*
154+
* @param sessionId The session id to be set. Must be a non-empty string.
155+
*/
156+
void setSessionId(@NonNull String sessionId);
157+
158+
/**
159+
* Get the session id to use within a non-native integration e.g. WebView.
160+
*
161+
* @return The session id or null if no session is present.
162+
*/
163+
@Nullable
164+
String getSessionId();
165+
151166
void prepareAttributesAsync(@NonNull Map<String, String> attributes);
152167

153168
enum KitStatus {

android-core/src/main/kotlin/com/mparticle/Rokt.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,31 @@ class Rokt internal constructor(private val mConfigManager: ConfigManager, priva
4343
mKitManager.close()
4444
}
4545
}
46+
47+
/**
48+
* Set the session id to use for the next execute call.
49+
*
50+
* This is useful for cases where you have a session id from a non-native integration,
51+
* e.g. WebView, and you want the session to be consistent across integrations.
52+
*
53+
* **Note:** Empty strings are ignored and will not update the session.
54+
*
55+
* @param sessionId The session id to be set. Must be a non-empty string.
56+
*/
57+
fun setSessionId(sessionId: String) {
58+
if (mConfigManager.isEnabled) {
59+
mKitManager.setSessionId(sessionId)
60+
}
61+
}
62+
63+
/**
64+
* Get the session id to use within a non-native integration e.g. WebView.
65+
*
66+
* @return The session id or null if no session is present or SDK is not initialized.
67+
*/
68+
fun getSessionId(): String? = if (mConfigManager.isEnabled) {
69+
mKitManager.getSessionId()
70+
} else {
71+
null
72+
}
4673
}

android-core/src/test/kotlin/com/mparticle/RoktTest.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import org.powermock.core.classloader.annotations.PrepareForTest
2424
import org.powermock.modules.junit4.PowerMockRunner
2525
import java.lang.ref.WeakReference
2626
import kotlin.test.assertEquals
27+
import kotlin.test.assertNull
2728
import kotlin.test.assertTrue
2829

2930
@RunWith(PowerMockRunner::class)
@@ -169,4 +170,35 @@ class RoktTest {
169170
assertTrue(elements.isEmpty())
170171
}
171172
}
173+
174+
@Test
175+
fun testSetSessionId_whenEnabled_delegatesToKitManager() {
176+
`when`(configManager.isEnabled).thenReturn(true)
177+
rokt.setSessionId("test-session-id")
178+
verify(kitManager).setSessionId("test-session-id")
179+
}
180+
181+
@Test
182+
fun testSetSessionId_whenDisabled_doesNotCallKitManager() {
183+
`when`(configManager.isEnabled).thenReturn(false)
184+
rokt.setSessionId("test-session-id")
185+
verify(kitManager, never()).setSessionId(any())
186+
}
187+
188+
@Test
189+
fun testGetSessionId_whenEnabled_delegatesToKitManager() {
190+
`when`(configManager.isEnabled).thenReturn(true)
191+
`when`(kitManager.getSessionId()).thenReturn("expected-session-id")
192+
val result = rokt.getSessionId()
193+
verify(kitManager).getSessionId()
194+
assertEquals("expected-session-id", result)
195+
}
196+
197+
@Test
198+
fun testGetSessionId_whenDisabled_returnsNull() {
199+
`when`(configManager.isEnabled).thenReturn(false)
200+
val result = rokt.getSessionId()
201+
verify(kitManager, never()).getSessionId()
202+
assertNull(result)
203+
}
172204
}

android-kit-base/src/main/java/com/mparticle/kits/KitIntegration.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,5 +641,22 @@ void enrichAttributes(
641641
void purchaseFinalized(@NonNull String placementId, @NonNull String catalogItemId, boolean status);
642642

643643
void close();
644+
645+
/**
646+
* Set the session id to use for the next execute call.
647+
* This is useful for cases where you have a session id from a non-native integration,
648+
* e.g. WebView, and you want the session to be consistent across integrations.
649+
*
650+
* @param sessionId The session id to be set. Must be a non-empty string.
651+
*/
652+
void setSessionId(@NonNull String sessionId);
653+
654+
/**
655+
* Get the session id to use within a non-native integration e.g. WebView.
656+
*
657+
* @return The session id or null if no session is present.
658+
*/
659+
@Nullable
660+
String getSessionId();
644661
}
645662
}

android-kit-base/src/main/java/com/mparticle/kits/KitManagerImpl.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,6 +1484,34 @@ public void close() {
14841484
}
14851485
}
14861486

1487+
@Override
1488+
public void setSessionId(@NonNull String sessionId) {
1489+
for (KitIntegration provider : providers.values()) {
1490+
try {
1491+
if (provider instanceof KitIntegration.RoktListener && !provider.isDisabled()) {
1492+
((KitIntegration.RoktListener) provider).setSessionId(sessionId);
1493+
}
1494+
} catch (Exception e) {
1495+
Logger.warning("Failed to call setSessionId for kit: " + provider.getName() + ": " + e.getMessage());
1496+
}
1497+
}
1498+
}
1499+
1500+
@Override
1501+
@Nullable
1502+
public String getSessionId() {
1503+
for (KitIntegration provider : providers.values()) {
1504+
try {
1505+
if (provider instanceof KitIntegration.RoktListener && !provider.isDisabled()) {
1506+
return ((KitIntegration.RoktListener) provider).getSessionId();
1507+
}
1508+
} catch (Exception e) {
1509+
Logger.warning("Failed to call getSessionId for kit: " + provider.getName() + ": " + e.getMessage());
1510+
}
1511+
}
1512+
return null;
1513+
}
1514+
14871515
@Override
14881516
public void prepareAttributesAsync(@NonNull Map<String, String> attributes) {
14891517

android-kit-base/src/test/kotlin/com/mparticle/kits/KitManagerImplTest.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2292,6 +2292,15 @@ class KitManagerImplTest {
22922292
override fun close() {
22932293
Logger.info("close called")
22942294
}
2295+
2296+
override fun setSessionId(sessionId: String) {
2297+
Logger.info("setSessionId called with $sessionId")
2298+
}
2299+
2300+
override fun getSessionId(): String? {
2301+
Logger.info("getSessionId called")
2302+
return null
2303+
}
22952304
}
22962305

22972306
internal inner class KitManagerEventCounter : MockKitManagerImpl() {

0 commit comments

Comments
 (0)