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

Add clearTimedEvent() and clearTimedEvents() #779

Merged
merged 2 commits into from
Mar 2, 2022
Merged
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
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=6.0.0
VERSION_NAME=6.0.1-SNAPSHOT
jaredmixpanel marked this conversation as resolved.
Show resolved Hide resolved

POM_PACKAGING=aar
GROUP=com.mixpanel.android
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -1423,6 +1424,42 @@ AnalyticsMessages getAnalyticsMessages() {
assertEquals(0, anonymousUpdates.size());
}

@Test
public void testEventTiming() throws InterruptedException {
final int MAX_TIMEOUT_POLL = 6500;
Future<SharedPreferences> mMockReferrerPreferences;
final BlockingQueue<String> mStoredEvents = new LinkedBlockingQueue<>();
mMockReferrerPreferences = new TestUtils.EmptyPreferences(InstrumentationRegistry.getInstrumentation().getContext());
MixpanelAPI mMixpanelAPI = new MixpanelAPI(InstrumentationRegistry.getInstrumentation().getContext(), mMockReferrerPreferences, "TESTTOKEN", false, null) {
@Override
PersistentIdentity getPersistentIdentity(Context context, Future<SharedPreferences> referrerPreferences, String token) {
mPersistentIdentity = super.getPersistentIdentity(context, referrerPreferences, token);
return mPersistentIdentity;
}

};

mMixpanelAPI.timeEvent("Time Event");
assertEquals(1, mPersistentIdentity.getTimeEvents().size());

mMixpanelAPI.track("Time Event");
assertEquals(0, mPersistentIdentity.getTimeEvents().size());
mMixpanelAPI.timeEvent("Time Event1");
mMixpanelAPI.timeEvent("Time Event2");
assertEquals(2, mPersistentIdentity.getTimeEvents().size());
mMixpanelAPI.clearTimedEvents();
assertEquals(0, mPersistentIdentity.getTimeEvents().size());
mMixpanelAPI.timeEvent("Time Event3");
mMixpanelAPI.timeEvent("Time Event4");
mMixpanelAPI.clearTimedEvent("Time Event3");
assertEquals(1, mPersistentIdentity.getTimeEvents().size());
assertTrue(mPersistentIdentity.getTimeEvents().containsKey("Time Event4"));
assertFalse(mPersistentIdentity.getTimeEvents().containsKey("Time Event3"));
mMixpanelAPI.clearTimedEvent(null);
assertEquals(1, mPersistentIdentity.getTimeEvents().size());
}


@Test
public void testSessionMetadata() throws InterruptedException, JSONException {
final BlockingQueue<JSONObject> storedJsons = new LinkedBlockingQueue<>();
Expand Down Expand Up @@ -1525,4 +1562,6 @@ protected void track(String eventName, JSONObject properties, boolean isAutomati
private static final int POLL_WAIT_SECONDS = 10;

private String mAppProperties;

private PersistentIdentity mPersistentIdentity;
}
27 changes: 25 additions & 2 deletions src/main/java/com/mixpanel/android/mpmetrics/MixpanelAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,29 @@ public void timeEvent(final String eventName) {
}
}

/**
* Clears all current event timings.
*
*/
public void clearTimedEvents() {
synchronized (mEventTimings) {
mEventTimings.clear();
mPersistentIdentity.clearTimedEvents();
}
}

/**
* Clears the event timing for an event.
*
* @param eventName the name of the timed event to clear.
*/
public void clearTimedEvent(final String eventName) {
synchronized (mEventTimings) {
mEventTimings.remove(eventName);
mPersistentIdentity.removeTimedEvent(eventName);
}
}

/**
* Retrieves the time elapsed for the named event since timeEvent() was called.
*
Expand Down Expand Up @@ -1007,7 +1030,7 @@ public void optOutTracking() {
mPersistentIdentity.clearPreferences();
synchronized (mEventTimings) {
mEventTimings.clear();
mPersistentIdentity.clearTimeEvents();
mPersistentIdentity.clearTimedEvents();
}
mPersistentIdentity.clearReferrerProperties();
mPersistentIdentity.setOptOutTracking(true, mToken);
Expand Down Expand Up @@ -1996,7 +2019,7 @@ protected void track(String eventName, JSONObject properties, boolean isAutomati
synchronized (mEventTimings) {
eventBegin = mEventTimings.get(eventName);
mEventTimings.remove(eventName);
mPersistentIdentity.removeTimeEvent(eventName);
mPersistentIdentity.removeTimedEvent(eventName);
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public synchronized void clearPreferences() {
}
}

public void clearTimeEvents() {
public void clearTimedEvents() {
try {
final SharedPreferences prefs = mTimeEventsPreferences.get();
final SharedPreferences.Editor editor = prefs.edit();
Expand Down Expand Up @@ -304,7 +304,7 @@ public Map<String, Long> getTimeEvents() {
}

// access is synchronized outside (mEventTimings)
public void removeTimeEvent(String timeEventName) {
public void removeTimedEvent(String timeEventName) {
try {
final SharedPreferences prefs = mTimeEventsPreferences.get();
final SharedPreferences.Editor editor = prefs.edit();
Expand Down