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

chore: support log item attributes and change visibility for item attribute #43

Merged
merged 2 commits into from
Dec 15, 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
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 @@ -134,10 +134,6 @@ class ClickstreamEvent: AnalyticsPropertiesModel {
}
return attribute
}

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

// MARK: - ClickstreamLogger
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 ""
}
}
11 changes: 0 additions & 11 deletions Tests/ClickstreamTests/Clickstream/ClickstreamEventTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,4 @@ class ClickstreamEventTest: XCTestCase {
XCTAssertTrue(errorValueString.contains(ClickstreamAnalytics.Item.ITEM_NAME))
XCTAssertEqual(0, clickstreamEvent.items.count)
}

func testEventEqualsFail() {
let event1 = clickstreamEvent!
let event2 = ClickstreamEvent(eventType: "testEvent",
appId: testAppId,
uniqueId: UUID().uuidString,
session: Session(uniqueId: UUID().uuidString, sessionIndex: 1),
systemInfo: SystemInfo(storage: storage),
netWorkType: NetWorkType.Wifi)
XCTAssertFalse(event1 == event2)
}
}
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