Skip to content

Commit

Permalink
Merge branch 'feature/itbl-track-anon-user' into sync_in_app_message_…
Browse files Browse the repository at this point in the history
…implementation
  • Loading branch information
evantk91 authored Oct 15, 2024
2 parents ec8877b + 13c2693 commit 69127a0
Show file tree
Hide file tree
Showing 45 changed files with 9,756 additions and 179 deletions.
124 changes: 124 additions & 0 deletions AnonymousUserEventTracking.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# AnonymousUserManager Class

## Class Introduction

The `AnonymousUserManager` class is responsible for managing anonymous user sessions and tracking events in an Android application.
It includes methods for updating sessions, tracking events, and handling criteria for transitioning to known users.

## Class Structure

The `AnonymousUserManager` class includes the following key components:

- **Fields:**
- `TAG`: A constant String for logging.

- **Methods:**
- `updateAnonSession()`: Updates the anonymous user session.
- `trackAnonEvent(String eventName, JSONObject dataFields)`: Tracks an anonymous event.
- `trackAnonPurchaseEvent(double total, List<CommerceItem> items, JSONObject dataFields)`: Tracks an anonymous purchase event.
- `trackAnonUpdateCart(List<CommerceItem> items)`: Tracks an anonymous cart update.
- `getCriteria()`: Retrieves criteria for user transition.
- `checkCriteriaCompletion()`: Checks if criteria for user transition are met.
- `createKnownUser()`: Creates a known user after criteria are met.
- `syncEvents()`: Syncs tracked events with the server.
- `clearLocallyStoredData()`: Clears locally stored data.
- `storeEventListToLocalStorage(JSONObject newDataObject)`: Stores event data in local storage.
- `getEventListFromLocalStorage()`: Retrieves event data from local storage.
- `getCurrentTime()`: Gets the current time in milliseconds.
- `getCurrentDateTime()`: Gets the current date and time in UTC format.

## Methods Description

### `updateAnonSession()`

This method updates the anonymous user session. It does the following:

* Retrieves the previous session data from SharedPreferences.
* Increments the session number.
* Stores the updated session data in SharedPreferences.

### `trackAnonEvent(eventName, dataFields)`

This method tracks an anonymous event. It does the following:

* Creates a JSON object with event details, including the event name, timestamp, data fields, and tracking type.
* Stores the event data in local storage.
* Checks criteria completion and creates a known user if criteria are met.

### `trackAnonPurchaseEvent(total, items, dataFields)`

This method tracks an anonymous purchase event. It does the following:

* Converts the list of commerce items to JSON.
* Creates a JSON object with purchase event details, including items, total, timestamp, data fields, and tracking type.
* Stores the purchase event data in local storage.
* Checks criteria completion and creates a known user if criteria are met.

### `trackAnonUpdateCart(items)`

This method tracks an anonymous cart update. It does the following:

* Converts the list of commerce items to JSON.
* Creates a JSON object with cart update details, including items, timestamp, and tracking type.
* Stores the cart update data in local storage.
* Checks criteria completion and creates a known user if criteria are met.

### `getCriteria()`

This method is responsible for fetching criteria data. It simulates calling an API and saving data in SharedPreferences.

### `checkCriteriaCompletion()`

This private method checks if criteria for creating a known user are met. It compares stored event data with predefined criteria and returns `true` if criteria are completed.

### `createKnownUser()`

This method is responsible for creating a known user in the Iterable API. It does the following:

* Sets a random user ID using a UUID (Universally Unique Identifier).
* Retrieves user session data from SharedPreferences.
* If user session data exists, it updates the user information in the Iterable API.
* Calls the syncEvents() method to synchronize tracked events.
* Finally, it clears locally stored data using the clearLocallyStoredData() method.

### `syncEvents()`

This method is used to synchronize tracked events stored in local storage with the Iterable API. It performs the following tasks:

* Retrieves the list of tracked events from local storage using the getEventListFromLocalStorage() method.
* Iterates through the list of events and processes each event based on its type.
* Supported event types include regular event tracking, purchase event tracking, and cart update tracking.
* For each event, it extracts relevant data, including event name, data fields, items (for purchase and cart update events), and timestamps.
* It uses the Iterable API to track these events or update the user's cart.
* After processing all events, it calls the clearLocallyStoredData() method to clear locally stored event data.

### `clearLocallyStoredData()`

This method is responsible for clearing locally stored data in SharedPreferences. It removes both the anonymous session data and the event list data, effectively cleaning up after successful synchronization with the Iterable API.

### `storeEventListToLocalStorage(JSONObject newDataObject)`

This method is used to add a new event to the list of events stored in local storage. It takes a JSON object representing the new event data and performs the following steps:

* Retrieves the existing list of events from local storage using the getEventListFromLocalStorage() method.
* Appends the new event data to the list.
* Updates the event list data in SharedPreferences.

### `getEventListFromLocalStorage()`

This method retrieves the list of tracked events from local storage in SharedPreferences. It returns a JSONArray containing the stored event data. If no data is found or an error occurs during retrieval, it returns an empty JSONArray.

### `getCurrentTime()`

This method returns the current timestamp as a long value, representing the number of milliseconds

## Usage

You can use the `AnonymousUserManager` class to manage anonymous user sessions, track events, and determine when to create known users based on predefined criteria.

```java
// Example usage
AnonymousUserManager userManager = new AnonymousUserManager();
userManager.updateAnonSession();
userManager.trackAnonEvent("UserLoggedIn", userData);
userManager.trackAnonPurchaseEvent(100.0, items, otherData);
38 changes: 38 additions & 0 deletions AnonymousUserMerge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# AnonymousUserMerge Class

## Class Introduction

The `AnonymousUserMerge` class is responsible for merging anonymous user with logged-in one.
It includes methods for merge user by userId and emailId.
We call methods of this class internally to merge user when setUserId or setEmail method call. After merge we sync events through Iterable API.

## Class Structure

The `AnonymousUserMerge` class includes the following key components:

- **Methods:**
- `mergeUserUsingUserId(apiClient: IterableApiClient, destinationUserId: String)`: Merge user using userID if anonymous user exists and sync events
- `mergeUserUsingEmail(apiClient: IterableApiClient, destinationEmail: String)`: Merge user using emailId if anonymous user exists and sync events
- `callMergeApi(apiClient: IterableApiClient, sourceEmail: String, sourceUserId: String, destinationEmail: String, destinationUserId: String)`: Call API to merge user and sync remaining events.

## Methods Description

### `mergeUserUsingUserId(apiClient: IterableApiClient, destinationUserId: String)`

This method merge the anonymous user with the logged-in one. It does the following:

* Check for user exists using userId.
* If user exists then call the merge user API.

### `mergeUserUsingEmail(apiClient: IterableApiClient, destinationEmail: String)`

This method merge the anonymous user with the logged-in one. It does the following:

* Check for user exists using emailId.
* If user exists then call the merge user API.

### `callMergeApi(apiClient: IterableApiClient, sourceEmail: String, sourceUserId: String, destinationEmail: String, destinationUserId: String)`

This method call API to merge user. It does the following:

* Call the Iterable API and sync remaining events.
135 changes: 135 additions & 0 deletions app/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ dependencies {
implementation 'androidx.multidex:multidex:2.0.1'
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'androidx.fragment:fragment:1.2.4'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
debugImplementation 'androidx.fragment:fragment-testing:1.2.4'

implementation project(':iterableapi')
Expand All @@ -78,7 +79,6 @@ dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-web:3.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.2.0'
androidTestImplementation 'br.com.concretesolutions:kappuccino:1.2.1'
}

tasks.withType(Test) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/androidTest/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:tools="http://schemas.android.com/tools">
<uses-sdk tools:overrideLibrary="br.com.concretesolutions.kappuccino,android_libs.ub_uiautomator,androidx.security"/>
<uses-sdk tools:overrideLibrary="android_libs.ub_uiautomator,androidx.security"/>
</manifest>
Loading

0 comments on commit 69127a0

Please sign in to comment.