Skip to content

Commit

Permalink
feat: auto recording screen views as well on the Flutter-Android Brid…
Browse files Browse the repository at this point in the history
…ge and minor improvements
  • Loading branch information
Desu Sai Venkat committed Nov 22, 2023
1 parent ede1ff3 commit 595913e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static com.rudderstack.sdk.flutter.parsers.RudderOptionsParser.getRudderOptionsObject;
import static com.rudderstack.sdk.flutter.parsers.RudderTraitsParser.getRudderTraitsObject;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.text.TextUtils;
Expand All @@ -17,6 +18,7 @@
import com.rudderstack.android.sdk.core.RudderOption;
import com.rudderstack.android.sdk.core.RudderProperty;
import com.rudderstack.android.sdk.core.RudderTraits;
import com.rudderstack.android.sdk.core.ScreenPropertyBuilder;
import com.rudderstack.sdk.flutter.managers.ActivityLifeCycleManager;
import com.rudderstack.sdk.flutter.managers.ApplicationLifeCycleManager;
import com.rudderstack.sdk.flutter.managers.PreferenceManager;
Expand All @@ -40,8 +42,13 @@ public class RudderSdkFlutterPlugin implements FlutterPlugin, MethodCallHandler
public static final String CATEGORY = "category";
private Context context;
private static RudderSdkFlutterPlugin instance;
private boolean autoTrackLifeCycleEvents = true;
private boolean autoRecordScreenViews = false;

private boolean autoSessionTracking = true;
private Long sessionTimeoutInMilliSeconds = 30000L;


private RudderConfig config;
private UserSessionManager userSessionManager;
private static List<RudderIntegration.Factory> integrationList;
/// The MethodChannel that will the communication between Flutter and native Android
Expand Down Expand Up @@ -130,31 +137,38 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {

private void initializeSDK(MethodCall call) {
initializeNativeSDK(call);
initializeBridgeSDK();
initializeBridgeSDK(call);
instance = this;
}

private void initializeNativeSDK(MethodCall call) {
Map<String, Object> argumentsMap = (Map<String, Object>) call.arguments;
String writeKey = (String) argumentsMap.get("writeKey");
Map<String, Object> configMap = (Map<String, Object>) argumentsMap.get("config");
config = getRudderConfigObject(configMap, dbEncryptionMap, integrationList);
RudderConfig config = getRudderConfigObject(configMap, dbEncryptionMap, integrationList);
RudderOption options = null;
if (argumentsMap.containsKey(OPTIONS)) {
options = getRudderOptionsObject((Map<String, Object>) argumentsMap.get(OPTIONS));
}
RudderClient.getInstance(context, writeKey, config, options);
}

private void initializeBridgeSDK() {
private void initializeBridgeSDK(MethodCall call) {
Map<String, Object> argumentsMap = (Map<String, Object>) call.arguments;
Map<String, Object> configMap = (Map<String, Object>) argumentsMap.get("config");
autoTrackLifeCycleEvents = (Boolean) configMap.get("trackLifecycleEvents");
autoRecordScreenViews = (Boolean) configMap.get("recordScreenViews");
sessionTimeoutInMilliSeconds = getLong(configMap.get("sessionTimeoutInMillis"));
autoSessionTracking = (Boolean) configMap.get("autoSessionTracking");

PreferenceManager preferenceManager = PreferenceManager.getInstance(context);
userSessionManager = new UserSessionManager(config.isTrackAutoSession(), config.isTrackLifecycleEvents(), preferenceManager, 300000);
userSessionManager = new UserSessionManager(autoSessionTracking, autoTrackLifeCycleEvents, preferenceManager, sessionTimeoutInMilliSeconds);
userSessionManager.handleAutoSessionTracking();
initiateLifeCycleManagers();
}

private void initiateLifeCycleManagers() {
ApplicationLifeCycleManager applicationLifeCycleManager = new ApplicationLifeCycleManager((Application) context, userSessionManager, config.isTrackLifecycleEvents());
ApplicationLifeCycleManager applicationLifeCycleManager = new ApplicationLifeCycleManager((Application) context, userSessionManager, autoTrackLifeCycleEvents);
applicationLifeCycleManager.trackApplicationLifeCycleEvents();
for (Runnable runnableTask : ActivityLifeCycleManager.runnableTasks) {
runnableTask.run();
Expand Down Expand Up @@ -271,7 +285,9 @@ private void startSession(MethodCall call) {
} else {
RudderClient.getInstance().startSession();
}
this.userSessionManager.clearAutoSessionStatus();
}

private void endSession() {
RudderClient.getInstance().endSession();
}
Expand Down Expand Up @@ -313,7 +329,7 @@ private static void getRudderContext(@NonNull Result result) {
}

public void trackApplicationOpened(boolean fromBackground) {
if (this.config.isTrackLifecycleEvents()) {
if (autoTrackLifeCycleEvents) {
RudderProperty property = new RudderProperty();
property.put("from_background", fromBackground);
RudderClient.getInstance().track("Application Opened", property);
Expand All @@ -322,19 +338,28 @@ public void trackApplicationOpened(boolean fromBackground) {
}

public void trackApplicationBackgrounded() {
if (this.config.isTrackLifecycleEvents()) {
if (autoTrackLifeCycleEvents) {
RudderClient.getInstance().track("Application Backgrounded");
this.userSessionManager.updateLastEventTimestamp();
}
}

public void trackScreen(Activity activity) {
if (autoRecordScreenViews) {
RudderProperty property = new RudderProperty();
property.put("automatic", true);
RudderClient.getInstance().screen(activity.getLocalClassName(), property);
this.userSessionManager.updateLastEventTimestamp();
}
}

@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
}

public Long getLong(Object obj) {
if(obj instanceof Long) {
if (obj instanceof Long) {
return (Long) obj;
} else {
return ((Integer) obj).longValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@ public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle saved
public void onActivityStarted(@NonNull Activity activity) {
if (noOfActivities.incrementAndGet() == 1) {
Runnable runnableTask = () -> RudderSdkFlutterPlugin.getInstance().trackApplicationOpened(fromBackground);
if (RudderSdkFlutterPlugin.getInstance() == null) {
runnableTasks.add(runnableTask);
} else {
runnableTask.run();
}
executeRunnable(runnableTask);
}
Runnable runnableTask = () -> RudderSdkFlutterPlugin.getInstance().trackScreen(activity);
executeRunnable(runnableTask);
}

@Override
Expand All @@ -65,11 +63,7 @@ public void onActivityStopped(@NonNull Activity activity) {
fromBackground = true;
if (noOfActivities.decrementAndGet() == 0) {
Runnable runnableTask = () -> RudderSdkFlutterPlugin.getInstance().trackApplicationBackgrounded();
if (RudderSdkFlutterPlugin.getInstance() == null) {
runnableTasks.add(runnableTask);
} else {
runnableTask.run();
}
executeRunnable(runnableTask);
}
}

Expand All @@ -84,4 +78,12 @@ public void onActivityDestroyed(@NonNull Activity activity) {
// No action needed in this method
// This method is intentionally left empty as there is no specific task to perform when the activity is destroyed.
}

private static void executeRunnable(Runnable runnableTask) {
if (RudderSdkFlutterPlugin.getInstance() == null) {
runnableTasks.add(runnableTask);
} else {
runnableTask.run();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ private void startAutoSessionIfNeeded() {
RudderLogger.logVerbose("UserSessionManager: startAutoSessionIfNeeded: Starting a new auto session");
startSession();
}
// save that an auto session exists to the preference manager
this.preferenceManager.saveAutoSessionExists(true);
persistAutoSessionStatus();
}


Expand All @@ -63,8 +62,7 @@ private void endAutoSessionIfExists() {
if (this.preferenceManager.doesAutoSessionExists()) {
RudderLogger.logVerbose("UserSessionManager: endAutoSessionIfExists: Clearing the existing automatic session");
endSession();
// clear that an auto session exists from the preference manager
this.preferenceManager.saveAutoSessionExists(false);
clearAutoSessionStatus();
}
}

Expand Down Expand Up @@ -123,6 +121,22 @@ void endSession() {
}


/**
* save that an auto session exists to the preference manager
*/
private void persistAutoSessionStatus() {
this.preferenceManager.saveAutoSessionExists(true);
}


/**
* clear that an auto session exists from the preference manager
*/

public void clearAutoSessionStatus() {
this.preferenceManager.saveAutoSessionExists(false);
}

@Nullable
Long getSessionId() {
return (RudderClient.getInstance() != null) ? RudderClient.getInstance().getSessionId() : null;
Expand Down

0 comments on commit 595913e

Please sign in to comment.