Skip to content

Commit

Permalink
Merge pull request #77 from Parsely/flush_events_on_app_on_stop
Browse files Browse the repository at this point in the history
  • Loading branch information
wzieba authored Oct 16, 2023
2 parents 2071aad + bed343b commit 7307e21
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 33 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ jobs:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: set up JDK 11
- name: set up JDK
uses: actions/setup-java@v3
with:
java-version: '11'
java-version: '17'
distribution: 'temurin'
cache: gradle
- name: Build library
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/readme.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ jobs:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: set up JDK 11
- name: set up JDK
uses: actions/setup-java@v3
with:
java-version: '11'
java-version: '17'
distribution: 'temurin'
cache: gradle
- name: Build library
Expand Down
8 changes: 0 additions & 8 deletions example/src/main/java/com/example/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,6 @@ public void run() {

}

@Override
protected void onDestroy() {
ParselyTracker.sharedInstance().flushEventQueue();
super.onDestroy();
}

private void updateEngagementStrings() {
StringBuilder eMsg = new StringBuilder("Engagement is ");
if (ParselyTracker.sharedInstance().engagementIsActive() == true) {
Expand Down Expand Up @@ -150,6 +144,4 @@ public void trackPause(View view) {
}

public void trackReset(View view) {ParselyTracker.sharedInstance().resetVideo(); }

public void flushEventQueue(View view) {ParselyTracker.sharedInstance().flushEventQueue(); }
}
11 changes: 1 addition & 10 deletions example/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,11 @@
android:onClick="trackReset"
android:text="@string/button_reset_video" />

<Button
android:id="@+id/flush_events_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/reset_video_button"
android:layout_centerHorizontal="true"
android:onClick="flushEventQueue"
android:text="@string/button_flush_event_queue" />

<TextView
android:id="@+id/queue_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/flush_events_button"
android:layout_below="@+id/reset_video_button"
android:layout_centerHorizontal="true"
android:text="Queued events: 0" />

Expand Down
1 change: 0 additions & 1 deletion example/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@
<string name="button_track_video">Track Video</string>
<string name="button_pause_video">Pause Video</string>
<string name="button_reset_video">Reset Video</string>
<string name="button_flush_event_queue">Flush Event Queue</string>

</resources>
3 changes: 3 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ org.gradle.jvmargs=-XX:MaxMetaspaceSize=2g

android.disableAutomaticComponentCreation=true
android.useAndroidX=true

android.experimental.lint.version=8.1.0

15 changes: 15 additions & 0 deletions parsely/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,24 @@ android {
}

dependencies {
def kotlinVersion = "1.8.10"

implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.3'
implementation 'com.google.android.gms:play-services-ads-identifier:18.0.1'
implementation 'androidx.lifecycle:lifecycle-process:2.6.2'
constraints {
add("implementation", "org.jetbrains.kotlin:kotlin-stdlib-jdk7") {
version {
require(kotlinVersion)
}
}
add("implementation", "org.jetbrains.kotlin:kotlin-stdlib-jdk8") {
version {
require(kotlinVersion)
}
}
}
}

apply from: "${rootProject.projectDir}/publication.gradle"
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import android.provider.Settings.Secure;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.ProcessLifecycleOwner;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.android.gms.ads.identifier.AdvertisingIdClient;
Expand Down Expand Up @@ -102,6 +105,14 @@ protected ParselyTracker(String siteId, int flushInterval, Context c) {
if (getStoredQueue().size() > 0) {
startFlushTimer();
}

ProcessLifecycleOwner.get().getLifecycle().addObserver(
(LifecycleEventObserver) (lifecycleOwner, event) -> {
if (event == Lifecycle.Event.ON_STOP) {
flushEvents();
}
}
);
}

/**
Expand Down Expand Up @@ -484,17 +495,12 @@ private void enqueueEvent(Map<String, Object> event) {
}

/**
* Flush events to Parsely.
* <p>
* Empties the event queue and sends the appropriate requests to Parsely.
* Called automatically after a number of seconds determined by {@link #getFlushInterval()}.
* <p>
* To make sure all of the queued events are flushed to Parse.ly's servers,
* call this method in your main activity's `onDestroy()` method.
* Deprecated since 3.1.1. The SDK now automatically flushes the queue on app lifecycle events.
* Any usage of this method is safe to remove and will have no effect. Keeping for backwards compatibility.
*/
@Deprecated
public void flushEventQueue() {
// needed for call from MainActivity
new FlushQueue().execute();
// no-op
}

/**
Expand Down Expand Up @@ -840,7 +846,7 @@ public void start() {

runningTask = new TimerTask() {
public void run() {
flushEventQueue();
flushEvents();
}
};
parentTimer.scheduleAtFixedRate(runningTask, intervalMillis, intervalMillis);
Expand All @@ -865,6 +871,10 @@ public long getIntervalMillis() {
}
}

private void flushEvents() {
new FlushQueue().execute();
}

/**
* Engagement manager for article and video engagement.
* <p>
Expand Down

0 comments on commit 7307e21

Please sign in to comment.