Skip to content

Commit

Permalink
feat: support log item and change item attribute visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoweii committed Dec 13, 2023
1 parent eb441ae commit 4c4b652
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 32 deletions.
45 changes: 30 additions & 15 deletions Sources/Clickstream/ClickstreamAnalytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,36 @@ public enum ClickstreamAnalytics {
Amplify.Analytics.enable()
}

/// ClickstreamAnalytics item attributes
/// ClickstreamAnalytics preset item attributes
/// In addition to the item attributes defined below, you can add up to 10 custom attributes to an item.
public enum Item {
static let ITEM_ID = "id"
static let ITEM_NAME = "name"
static let LOCATION_ID = "location_id"
static let ITEM_BRAND = "brand"
static let CURRENCY = "currency"
static let PRICE = "price"
static let QUANTITY = "quantity"
static let CREATIVE_NAME = "creative_name"
static let CREATIVE_SLOT = "creative_slot"
static let ITEM_CATEGORY = "item_category"
static let ITEM_CATEGORY2 = "item_category2"
static let ITEM_CATEGORY3 = "item_category3"
static let ITEM_CATEGORY4 = "item_category4"
static let ITEM_CATEGORY5 = "item_category5"
/// The id of the item
public static let ITEM_ID = "id"
/// The name of the item
public static let ITEM_NAME = "name"
/// The location id of the item
public static let LOCATION_ID = "location_id"
/// The brand of the item
public static let ITEM_BRAND = "brand"
/// The currency of the item
public static let CURRENCY = "currency"
/// The price of the item
public static let PRICE = "price"
/// The quantity of the item
public static let QUANTITY = "quantity"
/// The creative name of the item
public static let CREATIVE_NAME = "creative_name"
/// The creative slot of the item
public static let CREATIVE_SLOT = "creative_slot"
/// The category of the item
public static let ITEM_CATEGORY = "item_category"
/// The category2 of the item
public static let ITEM_CATEGORY2 = "item_category2"
/// The category3 of the item
public static let ITEM_CATEGORY3 = "item_category3"
/// The category4 of the item
public static let ITEM_CATEGORY4 = "item_category4"
/// The category5 of the item
public static let ITEM_CATEGORY5 = "item_category5"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ class EventRecorder: AnalyticsEventRecording {
/// save an clickstream event to storage
/// - Parameter event: A ClickstreamEvent
func save(_ event: ClickstreamEvent) throws {
let eventJson: String = event.toJson()
let eventObject = event.toJsonObject()
let eventJson = eventObject.toJsonString()
let eventSize = eventJson.count
let storageEvent = StorageEvent(eventJson: eventJson, eventSize: Int64(eventSize))
try dbUtil.saveEvent(storageEvent)
if clickstream.configuration.isLogEvents {
setLogLevel(logLevel: LogLevel.debug)
logEventPrettier(event: event)
log.debug("Saved event: \(event.eventType)\n\(eventObject.toPrettierJsonString())")
}
while try dbUtil.getTotalSize() > Constants.maxDbSize {
let events = try dbUtil.getEventsWith(limit: 5)
Expand Down Expand Up @@ -144,15 +145,6 @@ class EventRecorder: AnalyticsEventRecording {
}
return BatchEvent(eventsJson: eventsJson, eventCount: eventCount, lastEventId: lastEventId)
}

func logEventPrettier(event: ClickstreamEvent) {
var attributesStr = "attributes:{\n"
for (key, value) in event.attributes {
attributesStr += " \"\(key)\": \(value)\n"
}
attributesStr += "}"
log.debug("Event saved, event name: \(event.eventType)\n\(attributesStr)")
}
}

extension EventRecorder: ClickstreamLogger {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class ClickstreamEvent: AnalyticsPropertiesModel {
attributes[key]
}

func toJson() -> String {
func toJsonObject() -> JsonObject {
var event = JsonObject()
event["unique_id"] = uniqueId
event["event_type"] = eventType
Expand Down Expand Up @@ -111,7 +111,7 @@ class ClickstreamEvent: AnalyticsPropertiesModel {
}
event["user"] = userAttributes
event["attributes"] = getAttributeObject(from: attributes)
return event.toJsonString()
return event
}

private func getAttributeObject(from dictionary: AnalyticsProperties) -> JsonObject {
Expand All @@ -136,7 +136,7 @@ class ClickstreamEvent: AnalyticsPropertiesModel {
}

static func == (lhs: ClickstreamEvent, rhs: ClickstreamEvent) -> Bool {
lhs.toJson() == rhs.toJson()
lhs.toJsonObject().toJsonString() == rhs.toJsonObject().toJsonString()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,14 @@ extension JsonObject {
}
return ""
}

func toPrettierJsonString() -> String {
do {
let jsonData = try JSONSerialization.data(withJSONObject: self, options: [.sortedKeys, .prettyPrinted])
return String(data: jsonData, encoding: .utf8) ?? ""
} catch {
print("Error serializing dictionary to JSON: \(error.localizedDescription)")
}
return ""
}
}
2 changes: 1 addition & 1 deletion Tests/ClickstreamTests/Clickstream/EventRecorderTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ class EventRecorderTest: XCTestCase {

func testVerifyHashCodeInRequestParameter() {
clickstream.configuration.endpoint = testHashCodeEndpoint
let eventJson = "[" + clickstreamEvent.toJson() + "]"
let eventJson = "[" + clickstreamEvent.toJsonObject().toJsonString() + "]"
let eventJsonHashCode = eventJson.hashCode()
server["/collect/hashcode"] = { request in
let queryParams = request.queryParams
Expand Down
4 changes: 2 additions & 2 deletions Tests/ClickstreamTests/DataBase/ClickstreamDBUtilTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class ClickstreamDBUtiltest: XCTestCase {
session: Session(uniqueId: UUID().uuidString, sessionIndex: 1),
systemInfo: SystemInfo(storage: storage),
netWorkType: NetWorkType.Wifi)
let eventJson = clickstreamEvent.toJson()
storageEvent = StorageEvent(eventJson: clickstreamEvent.toJson(), eventSize: Int64(eventJson.count))
let eventJson = clickstreamEvent.toJsonObject().toJsonString()
storageEvent = StorageEvent(eventJson: clickstreamEvent.toJsonObject().toJsonString(), eventSize: Int64(eventJson.count))
} catch {
XCTFail("Fail to setup dbUtil error:\(error)")
}
Expand Down

0 comments on commit 4c4b652

Please sign in to comment.