diff --git a/Sources/Clickstream/ClickstreamAnalytics.swift b/Sources/Clickstream/ClickstreamAnalytics.swift index 3371825..8527fa2 100644 --- a/Sources/Clickstream/ClickstreamAnalytics.swift +++ b/Sources/Clickstream/ClickstreamAnalytics.swift @@ -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" } } diff --git a/Sources/Clickstream/Dependency/Clickstream/Analytics/EventRecorder.swift b/Sources/Clickstream/Dependency/Clickstream/Analytics/EventRecorder.swift index 8de1ee8..1330ad5 100644 --- a/Sources/Clickstream/Dependency/Clickstream/Analytics/EventRecorder.swift +++ b/Sources/Clickstream/Dependency/Clickstream/Analytics/EventRecorder.swift @@ -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) @@ -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 {} diff --git a/Sources/Clickstream/Dependency/Clickstream/Event/ClickstreamEvent.swift b/Sources/Clickstream/Dependency/Clickstream/Event/ClickstreamEvent.swift index 4a472db..21bf7b1 100644 --- a/Sources/Clickstream/Dependency/Clickstream/Event/ClickstreamEvent.swift +++ b/Sources/Clickstream/Dependency/Clickstream/Event/ClickstreamEvent.swift @@ -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 @@ -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 { @@ -136,7 +136,7 @@ class ClickstreamEvent: AnalyticsPropertiesModel { } static func == (lhs: ClickstreamEvent, rhs: ClickstreamEvent) -> Bool { - lhs.toJson() == rhs.toJson() + lhs.toJsonObject().toJsonString() == rhs.toJsonObject().toJsonString() } } diff --git a/Sources/Clickstream/Support/Extension/JsonObject+ToJsonString.swift b/Sources/Clickstream/Support/Extension/JsonObject+ToJsonString.swift index e96372c..0685cfd 100644 --- a/Sources/Clickstream/Support/Extension/JsonObject+ToJsonString.swift +++ b/Sources/Clickstream/Support/Extension/JsonObject+ToJsonString.swift @@ -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 "" + } } diff --git a/Tests/ClickstreamTests/Clickstream/EventRecorderTest.swift b/Tests/ClickstreamTests/Clickstream/EventRecorderTest.swift index 645e7c2..45dd280 100644 --- a/Tests/ClickstreamTests/Clickstream/EventRecorderTest.swift +++ b/Tests/ClickstreamTests/Clickstream/EventRecorderTest.swift @@ -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 diff --git a/Tests/ClickstreamTests/DataBase/ClickstreamDBUtilTest.swift b/Tests/ClickstreamTests/DataBase/ClickstreamDBUtilTest.swift index 5c38c97..bcfdba4 100644 --- a/Tests/ClickstreamTests/DataBase/ClickstreamDBUtilTest.swift +++ b/Tests/ClickstreamTests/DataBase/ClickstreamDBUtilTest.swift @@ -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)") }