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

feat: add configuration for user engagement event #34

Merged
merged 1 commit into from
Aug 24, 2023
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ ClickstreamAnalytics.getClickStreamConfiguration()
.withSendEventsInterval(10000)
.withSessionTimeoutDuration(1800000)
.withTrackScreenViewEvents(false)
.withTrackUserEngagementEvents(false)
.withTrackAppExceptionEvents(false)
.withLogEvents(true)
.withCustomDns(CustomOkhttpDns.getInstance())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,6 @@ public void configure(
pluginConfiguration.getBoolean(ConfigurationKey.COMPRESS_EVENTS.getConfigurationKey()));
}

if (pluginConfiguration.has(ConfigurationKey.TRACK_APP_LIFECYCLE_EVENTS.getConfigurationKey())) {
configurationBuilder.withTrackScreenViewEvents(pluginConfiguration
.getBoolean(ConfigurationKey.TRACK_APP_LIFECYCLE_EVENTS.getConfigurationKey()));
}

if (pluginConfiguration.has(ConfigurationKey.TRACK_APP_EXCEPTION_EVENTS.getConfigurationKey())) {
configurationBuilder.withTrackAppExceptionEvents(pluginConfiguration
.getBoolean(ConfigurationKey.TRACK_APP_EXCEPTION_EVENTS.getConfigurationKey()));
Expand Down Expand Up @@ -235,11 +230,6 @@ public enum ConfigurationKey {
*/
COMPRESS_EVENTS("isCompressEvents"),

/**
* Whether to track app lifecycle events automatically.
*/
TRACK_APP_LIFECYCLE_EVENTS("isTrackAppLifecycleEvents"),

/**
* Whether to track app exception events automatically.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ public final class AWSClickstreamPluginConfiguration {
private final long sendEventsInterval;
private final long callTimeOut;
private final boolean isTrackScreenViewEvents;
private final boolean isTrackUserEngagementEvents;
private final boolean isTrackAppExceptionEvents;
private final boolean isCompressEvents;
private final long sessionTimeOut;

private AWSClickstreamPluginConfiguration(Builder builder) {
this.appId = builder.appId;
this.isTrackScreenViewEvents = builder.isTrackScreenViewEvents;
this.isTrackUserEngagementEvents = builder.isTrackUserEngagementEvents;
this.isTrackAppExceptionEvents = builder.isTrackAppExceptionEvents;
this.callTimeOut = builder.callTimeOut;
this.sendEventsInterval = builder.sendEventsInterval;
Expand Down Expand Up @@ -80,6 +82,15 @@ boolean isTrackScreenViewEvents() {
return isTrackScreenViewEvents;
}

/**
* Is auto user engagement tracking enabled.
*
* @return Is auto user engagement tracking enabled.
*/
boolean isTrackUserEngagementEvents() {
return isTrackUserEngagementEvents;
}

/**
* Is auto exception tracking enabled.
*
Expand Down Expand Up @@ -136,6 +147,7 @@ static final class Builder {
private final long callTimeOut = DEFAULT_CALL_TIME_OUT;
private boolean isCompressEvents = true;
private boolean isTrackScreenViewEvents = true;
private boolean isTrackUserEngagementEvents = true;
private boolean isTrackAppExceptionEvents = false;

private long sessionTimeOut = DEFAULT_SESSION_TIME_OUT;
Expand Down Expand Up @@ -165,6 +177,11 @@ Builder withTrackScreenViewEvents(final boolean trackScreenViewEvents) {
return this;
}

Builder withTrackUserEngagementEvents(final boolean trackUserEngagementEvents) {
this.isTrackUserEngagementEvents = trackUserEngagementEvents;
return this;
}

Builder withTrackAppExceptionEvents(final boolean trackAppExceptionEvents) {
this.isTrackAppExceptionEvents = trackAppExceptionEvents;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ static ClickstreamManager create(Context context,
.withCallTimeOut(clickstreamPluginConfiguration.getCallTimeOut())
.withCompressEvents(clickstreamPluginConfiguration.isCompressEvents())
.withTrackScreenViewEvents(clickstreamPluginConfiguration.isTrackScreenViewEvents())
.withTrackUserEngagementEvents(clickstreamPluginConfiguration.isTrackUserEngagementEvents())
.withTrackAppExceptionEvents(clickstreamPluginConfiguration.isTrackAppExceptionEvents())
.withSessionTimeoutDuration(clickstreamPluginConfiguration.getSessionTimeOut());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ public String getScreenUniqueId(Activity activity) {
*/
public void recordUserEngagement() {
lastEngageTime = endEngageTimestamp - startEngageTimestamp;
if (lastEngageTime > MIN_ENGAGEMENT_TIME) {
if (clickstreamContext.getClickstreamConfiguration().isTrackUserEngagementEvents() &&
lastEngageTime > MIN_ENGAGEMENT_TIME) {
final AnalyticsEvent event =
this.clickstreamContext.getAnalyticsClient().createEvent(Event.PresetEvent.USER_ENGAGEMENT);
event.addAttribute(Event.ReservedAttribute.ENGAGEMENT_TIMESTAMP, lastEngageTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class ClickstreamConfiguration {
private long callTimeOut;
private boolean isCompressEvents;
private boolean isTrackScreenViewEvents;
private boolean isTrackUserEngagementEvents;
private boolean isTrackAppExceptionEvents;
private boolean isLogEvents;
private String authCookie;
Expand Down Expand Up @@ -192,6 +193,15 @@ public boolean isTrackScreenViewEvents() {
return this.isTrackScreenViewEvents;
}

/**
* Is track user engagement events.
*
* @return Is track user engagement events.
*/
public boolean isTrackUserEngagementEvents() {
return this.isTrackUserEngagementEvents;
}

/**
* Is track app screen view events.
*
Expand All @@ -203,6 +213,17 @@ public ClickstreamConfiguration withTrackScreenViewEvents(final boolean isTrackS
return this;
}

/**
* Is track user engagement events.
*
* @param isTrackUserEngagementEvents Is track user engagement events.
* @return the current ClickstreamConfiguration instance.
*/
public ClickstreamConfiguration withTrackUserEngagementEvents(final boolean isTrackUserEngagementEvents) {
this.isTrackUserEngagementEvents = isTrackUserEngagementEvents;
return this;
}

/**
* Is track app exception events.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ public void setup() {
AWSClickstreamPluginConfiguration.Builder configurationBuilder = AWSClickstreamPluginConfiguration.builder();
configurationBuilder.withAppId("demo-app")
.withEndpoint("http://cs-se-serve-1qtj719j88vwn-1291141553.ap-southeast-1.elb.amazonaws.com/collect")
.withSendEventsInterval(10000).withTrackScreenViewEvents(true);
.withSendEventsInterval(10000)
.withTrackScreenViewEvents(true)
.withTrackUserEngagementEvents(true);
AWSClickstreamPluginConfiguration clickstreamPluginConfiguration = configurationBuilder.build();
ClickstreamManager clickstreamManager =
ClickstreamManagerFactory.create(context, clickstreamPluginConfiguration);
Expand Down Expand Up @@ -146,6 +148,28 @@ public void testUserEngagementFail() throws Exception {
}
}

/**
* test record user engagement event when configure is disabled.
*
* @throws Exception exception.
*/
@Test
public void testCloseUserEngagementEvent() throws Exception {
clickstreamContext.getClickstreamConfiguration().withTrackUserEngagementEvents(false);
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START);
Thread.sleep(1100);
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
try (Cursor cursor = dbUtil.queryAllEvents();) {
List<String> eventList = new ArrayList<>();
while (cursor.moveToNext()) {
String eventString = cursor.getString(2);
JSONObject jsonObject = new JSONObject(eventString);
eventList.add(jsonObject.getString("event_type"));
}
assertFalse(eventList.contains(Event.PresetEvent.USER_ENGAGEMENT));
}
}

/**
* test view only one screen and record an screen view event.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public void setup() {
AWSClickstreamPluginConfiguration.Builder configurationBuilder = AWSClickstreamPluginConfiguration.builder();
configurationBuilder.withAppId("demo-app")
.withEndpoint("http://cs-se-serve-1qtj719j88vwn-1291141553.ap-southeast-1.elb.amazonaws.com/collect")
.withSendEventsInterval(10000);
.withSendEventsInterval(10000)
.withSessionTimeOut(1800000L);
AWSClickstreamPluginConfiguration clickstreamPluginConfiguration = configurationBuilder.build();
ClickstreamManager clickstreamManager =
ClickstreamManagerFactory.create(context, clickstreamPluginConfiguration);
Expand Down
Loading