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 anonevents #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
106 changes: 106 additions & 0 deletions AnonymousUserEventTracking.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# AnonymousUserManager Class

## Class Introduction

The `AnonymousUserManager` class is responsible for managing anonymous user sessions and tracking events.
It includes methods for updating sessions, tracking events (i.e regular, update cart and purchase) and create a user if criterias are met.
We call track methods of this class internally to make sure we have tracked the events even when user is NOT logged in and after certain criterias are met we create a user and logs them automatically and sync events through Iterable API.

## Class Structure

The `AnonymousUserManager` class includes the following key components:

- **Methods:**
- `updateAnonSession()`: Updates the anonymous user session.
- `trackAnonEvent(name: String, dataFields: [AnyHashable: Any]?)`: Tracks an anonymous event and store it locally.
- `trackAnonPurchaseEvent(total: NSNumber, items: [CommerceItem], dataFields: [AnyHashable: Any]?)`: Tracks an anonymous purchase event and store it locally.
- `trackAnonUpdateCart(items: [CommerceItem])`: Tracks an anonymous cart event and store it locally.
- `trackAnonTokenRegistration(token: String)`: Tracks an anonymous token registration event and store it locally.
- `getAnonCriteria()`: Gets the anonymous criteria.
- `checkCriteriaCompletion()`: Checks if criterias are being met.
- `createKnownUser()`: Creates a user after criterias met and login the user and then sync the data through track APIs.
- `syncEvents()`: Syncs locally saved data through track APIs.
- `updateAnonSession()`: Stores an anonymous sessions locally. Update the last session time when new session is created.
- `storeEventData()`: Stores event data locally.
- `logout()`: Reset the locally saved data when user logs out to make sure no old data is left.
- `syncNonSyncedEvents()`: Syncs unsynced data which might have failed to sync when calling syncEvents for the first time after criterias met.
- `convertCommerceItems(from dictionaries: [[AnyHashable: Any]]) -> [CommerceItem]`: Convert to commerce items from dictionaries.
- `convertCommerceItemsToDictionary(_ items: [CommerceItem]) -> [[AnyHashable:Any]]`: Convert commerce items to dictionaries.
- `filterEvents(byType type: String) -> [[AnyHashable: Any]]?`: Filter events by type.
- `filterEvents(byType type: String, andName name: String?) -> [[AnyHashable: Any]]?`: Filter events by type and name.
- `getUTCDateTime()`: Converts UTC Datetime from current time.
- `filterEvents(excludingTimestamps excludedTimestamps: [Int]) -> [[AnyHashable: Any]]?`: Filter non-synced data.


## Methods Description

### `updateAnonSession()`

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

* Retrieves the previous session data from local storage.
* Increments the session number.
* Stores the updated session data back to local storage.

### `trackAnonEvent(name: String, dataFields: [AnyHashable: Any]?)`

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

* Creates a dictionary 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: NSNumber, items: [CommerceItem], dataFields: [AnyHashable: Any]?)`

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

* Converts the list of commerce items to JSON.
* Creates a dictionary 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: [CommerceItem])`

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

* Converts the list of commerce items to dictionary.
* Creates a dictionary 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.

### `trackAnonTokenRegistration(token: String)`

This method tracks an anonymous token registration event and stores it locally.

### `getAnonCriteria()`

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

### `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 local storage.
* If user session data exists, it updates the user information in the Iterable API.
* Calls the syncEvents() method to synchronize anonymous tracked events.
* Finally, it clears locally stored data after data is syncronized.

### `syncEvents()`

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

* Retrieves the list of tracked events from local storage.
* 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 then calls the Iterable API to sync these events.
* After processing all the events, it clears locally stored event data.

### `updateAnonSession()`

This method is responsible for storing/updating anonymous sessions locally. It updates the last session time each time when new session is created.
4 changes: 4 additions & 0 deletions swift-sdk.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
55E02D39253F8D86009DB8BC /* WebViewProtocolTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55E02D38253F8D86009DB8BC /* WebViewProtocolTests.swift */; };
55E6F462238E066400808BCE /* DeepLinkTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55E6F45E238E066400808BCE /* DeepLinkTests.swift */; };
55E9BE3429F9F5E6000C9FF2 /* DependencyContainerProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55E9BE3329F9F5E6000C9FF2 /* DependencyContainerProtocol.swift */; };
5692A92E2AA99E2C0066C524 /* AnonymousUserManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5692A92D2AA99E2C0066C524 /* AnonymousUserManager.swift */; };
5B49BB3E27CFB71500E6F00C /* PopupInboxSessionUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B49BB3D27CFB71500E6F00C /* PopupInboxSessionUITests.swift */; };
5B5AA711284F1A6D0093FED4 /* MockNetworkSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B5AA710284F1A6D0093FED4 /* MockNetworkSession.swift */; };
5B5AA712284F1A6D0093FED4 /* MockNetworkSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B5AA710284F1A6D0093FED4 /* MockNetworkSession.swift */; };
Expand Down Expand Up @@ -563,6 +564,7 @@
55E02D38253F8D86009DB8BC /* WebViewProtocolTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebViewProtocolTests.swift; sourceTree = "<group>"; };
55E6F45E238E066400808BCE /* DeepLinkTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DeepLinkTests.swift; sourceTree = "<group>"; };
55E9BE3329F9F5E6000C9FF2 /* DependencyContainerProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DependencyContainerProtocol.swift; sourceTree = "<group>"; };
5692A92D2AA99E2C0066C524 /* AnonymousUserManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AnonymousUserManager.swift; sourceTree = "<group>"; };
5B49BB3D27CFB71500E6F00C /* PopupInboxSessionUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PopupInboxSessionUITests.swift; sourceTree = "<group>"; };
5B5AA710284F1A6D0093FED4 /* MockNetworkSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockNetworkSession.swift; sourceTree = "<group>"; };
5B6C3C1027CE871F00B9A753 /* NavInboxSessionUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NavInboxSessionUITests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -966,6 +968,7 @@
AC44C0EB22615F8100E0641D /* Resources */,
AC2263F920CF4B63009800EB /* Supporting Files */,
AC72A0C120CF4CB8004D7997 /* CommerceItem.swift */,
5692A92D2AA99E2C0066C524 /* AnonymousUserManager.swift */,
AC72A0BE20CF4CB8004D7997 /* Constants.swift */,
AC72A0BF20CF4CB8004D7997 /* IterableAction.swift */,
ACF560E720E55A6B000AAC23 /* IterableActionContext.swift */,
Expand Down Expand Up @@ -1995,6 +1998,7 @@
AC32E16821DD55B900BD4F83 /* OrderedDictionary.swift in Sources */,
ACF406232507BC72005FD775 /* NetworkMonitor.swift in Sources */,
AC1712892416AEF400F2BB0E /* WebViewProtocol.swift in Sources */,
5692A92E2AA99E2C0066C524 /* AnonymousUserManager.swift in Sources */,
AC3C10F9213F46A900A9B839 /* IterableLogging.swift in Sources */,
55DD207F26A0D83800773CC7 /* IterableAuthManagerProtocol.swift in Sources */,
ACE34AB72139D70B00691224 /* LocalStorageProtocol.swift in Sources */,
Expand Down
Loading
Loading