Skip to content

Commit

Permalink
Merge pull request #838 from mixpanel/jared-sr-broadcast-receiver
Browse files Browse the repository at this point in the history
Add SessionReplayBroadcastReceiver
  • Loading branch information
jaredmixpanel authored Aug 29, 2024
2 parents f40e1f7 + bf72d45 commit 43f8c94
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ allprojects {
// have to change the bit in uploadArchives that marks all dependencies as optional.
dependencies {
implementation "androidx.annotation:annotation:1.1.0"

implementation 'androidx.core:core:1.12.0'

// AndroidJUnitRunner and JUnit Rules
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:core:1.4.0'
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/com/mixpanel/android/mpmetrics/MixpanelAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import androidx.core.content.ContextCompat;

import com.mixpanel.android.util.MPConstants.SessionReplay;
import com.mixpanel.android.util.MPLog;
import com.mixpanel.android.util.ProxyServerInteractor;

Expand Down Expand Up @@ -105,7 +107,6 @@ public class MixpanelAPI {
*/
public static final String VERSION = MPConfig.VERSION;


/**
* You shouldn't instantiate MixpanelAPI objects directly.
* Use MixpanelAPI.getInstance to get an instance.
Expand Down Expand Up @@ -193,6 +194,14 @@ public class MixpanelAPI {
if (mConfig.getRemoveLegacyResidualFiles()) {
mMessages.removeResidualImageFiles(new File(mContext.getApplicationInfo().dataDir));
}

BroadcastReceiver sessionReplayReceiver = new SessionReplayBroadcastReceiver(this);
ContextCompat.registerReceiver(
mContext.getApplicationContext(),
sessionReplayReceiver,
SessionReplayBroadcastReceiver.INTENT_FILTER,
ContextCompat.RECEIVER_NOT_EXPORTED
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.mixpanel.android.mpmetrics;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import com.mixpanel.android.util.MPConstants.SessionReplay;
import com.mixpanel.android.util.MPLog;

import java.io.Serializable;
import java.util.HashMap;

public class SessionReplayBroadcastReceiver extends BroadcastReceiver {
private static final String LOGTAG = "SessionReplayBroadcastReceiver";
private final MixpanelAPI sdkInstance;

public static final IntentFilter INTENT_FILTER = new IntentFilter();
static {
INTENT_FILTER.addAction(SessionReplay.REGISTER_ACTION);
INTENT_FILTER.addAction(SessionReplay.UNREGISTER_ACTION);
}

public SessionReplayBroadcastReceiver(MixpanelAPI instance) {
this.sdkInstance = instance;
}

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (SessionReplay.REGISTER_ACTION.equals(action)) {
HashMap<String, Object> data = null;
Serializable serializableData = intent.getSerializableExtra("data");
if (serializableData instanceof HashMap) {
try {
data = (HashMap<String, Object>) serializableData;
} catch (ClassCastException e) {
MPLog.e(LOGTAG, "Failed to cast broadcast extras data to HashMap", e);
MPLog.d(LOGTAG, "Broadcast extras data: " + serializableData);
}
}
if (data != null && data.containsKey(SessionReplay.REPLAY_ID_KEY)) {
sdkInstance.registerSuperPropertiesMap(data);
}
} else if (SessionReplay.UNREGISTER_ACTION.equals(action)) {
sdkInstance.unregisterSuperProperty(SessionReplay.REPLAY_ID_KEY);
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/mixpanel/android/util/MPConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
*/

public class MPConstants {
public static class SessionReplay {
public static final String REGISTER_ACTION = "com.mixpanel.properties.register";
public static final String UNREGISTER_ACTION = "com.mixpanel.properties.unregister";
public static final String REPLAY_ID_KEY = "$mp_replay_id";
}
public static class URL {
public static final String MIXPANEL_API = "https://api.mixpanel.com";
public static final String EVENT = "/track/";
Expand Down

0 comments on commit 43f8c94

Please sign in to comment.