Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,22 @@ public void prepareAttributesAsync(@NonNull Map<String, String> attributes) {
}
}

@Override
public void setSessionId(@NonNull String sessionId) {
if (mKitManager != null) {
mKitManager.setSessionId(sessionId);
}
}

@Override
@Nullable
public String getSessionId() {
if (mKitManager != null) {
return mKitManager.getSessionId();
}
return null;
}

static class CoreCallbacksImpl implements CoreCallbacks {
KitFrameworkWrapper mKitFrameworkWrapper;
ConfigManager mConfigManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,21 @@ void execute(@NonNull String identifier,

void close();

/**
* Set the session id to use for the next execute call.
*
* @param sessionId The session id to be set. Must be a non-empty string.
*/
void setSessionId(@NonNull String sessionId);

/**
* Get the session id to use within a non-native integration e.g. WebView.
*
* @return The session id or null if no session is present.
*/
@Nullable
String getSessionId();

void prepareAttributesAsync(@NonNull Map<String, String> attributes);

enum KitStatus {
Expand Down
27 changes: 27 additions & 0 deletions android-core/src/main/kotlin/com/mparticle/Rokt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,31 @@ class Rokt internal constructor(private val mConfigManager: ConfigManager, priva
mKitManager.close()
}
}

/**
* Set the session id to use for the next execute call.
*
* This is useful for cases where you have a session id from a non-native integration,
* e.g. WebView, and you want the session to be consistent across integrations.
*
* **Note:** Empty strings are ignored and will not update the session.
*
* @param sessionId The session id to be set. Must be a non-empty string.
*/
fun setSessionId(sessionId: String) {
if (mConfigManager.isEnabled) {
mKitManager.setSessionId(sessionId)
}
}

/**
* Get the session id to use within a non-native integration e.g. WebView.
*
* @return The session id or null if no session is present or SDK is not initialized.
*/
fun getSessionId(): String? = if (mConfigManager.isEnabled) {
mKitManager.getSessionId()
} else {
null
}
}
32 changes: 32 additions & 0 deletions android-core/src/test/kotlin/com/mparticle/RoktTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
import java.lang.ref.WeakReference
import kotlin.test.assertEquals
import kotlin.test.assertNull
import kotlin.test.assertTrue

@RunWith(PowerMockRunner::class)
Expand Down Expand Up @@ -169,4 +170,35 @@ class RoktTest {
assertTrue(elements.isEmpty())
}
}

@Test
fun testSetSessionId_whenEnabled_delegatesToKitManager() {
`when`(configManager.isEnabled).thenReturn(true)
rokt.setSessionId("test-session-id")
verify(kitManager).setSessionId("test-session-id")
}

@Test
fun testSetSessionId_whenDisabled_doesNotCallKitManager() {
`when`(configManager.isEnabled).thenReturn(false)
rokt.setSessionId("test-session-id")
verify(kitManager, never()).setSessionId(any())
}

@Test
fun testGetSessionId_whenEnabled_delegatesToKitManager() {
`when`(configManager.isEnabled).thenReturn(true)
`when`(kitManager.getSessionId()).thenReturn("expected-session-id")
val result = rokt.getSessionId()
verify(kitManager).getSessionId()
assertEquals("expected-session-id", result)
}

@Test
fun testGetSessionId_whenDisabled_returnsNull() {
`when`(configManager.isEnabled).thenReturn(false)
val result = rokt.getSessionId()
verify(kitManager, never()).getSessionId()
assertNull(result)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -641,5 +641,22 @@ void enrichAttributes(
void purchaseFinalized(@NonNull String placementId, @NonNull String catalogItemId, boolean status);

void close();

/**
* Set the session id to use for the next execute call.
* This is useful for cases where you have a session id from a non-native integration,
* e.g. WebView, and you want the session to be consistent across integrations.
*
* @param sessionId The session id to be set. Must be a non-empty string.
*/
void setSessionId(@NonNull String sessionId);

/**
* Get the session id to use within a non-native integration e.g. WebView.
*
* @return The session id or null if no session is present.
*/
@Nullable
String getSessionId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1484,6 +1484,34 @@ public void close() {
}
}

@Override
public void setSessionId(@NonNull String sessionId) {
for (KitIntegration provider : providers.values()) {
try {
if (provider instanceof KitIntegration.RoktListener && !provider.isDisabled()) {
((KitIntegration.RoktListener) provider).setSessionId(sessionId);
}
} catch (Exception e) {
Logger.warning("Failed to call setSessionId for kit: " + provider.getName() + ": " + e.getMessage());
}
}
}

@Override
@Nullable
public String getSessionId() {
for (KitIntegration provider : providers.values()) {
try {
if (provider instanceof KitIntegration.RoktListener && !provider.isDisabled()) {
return ((KitIntegration.RoktListener) provider).getSessionId();
}
} catch (Exception e) {
Logger.warning("Failed to call getSessionId for kit: " + provider.getName() + ": " + e.getMessage());
}
}
return null;
}

@Override
public void prepareAttributesAsync(@NonNull Map<String, String> attributes) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2292,6 +2292,15 @@ class KitManagerImplTest {
override fun close() {
Logger.info("close called")
}

override fun setSessionId(sessionId: String) {
Logger.info("setSessionId called with $sessionId")
}

override fun getSessionId(): String? {
Logger.info("getSessionId called")
return null
}
}

internal inner class KitManagerEventCounter : MockKitManagerImpl() {
Expand Down
Loading