Skip to content

Commit 3287e77

Browse files
zhu-xiaoweixiaoweii
andauthored
chore: support log item attributes and change visibility for item attribute (#43)
Co-authored-by: xiaoweii <xiaoweii@amazom.com>
1 parent c18458c commit 3287e77

File tree

7 files changed

+48
-46
lines changed

7 files changed

+48
-46
lines changed

Sources/Clickstream/ClickstreamAnalytics.swift

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,36 @@ public enum ClickstreamAnalytics {
8989
Amplify.Analytics.enable()
9090
}
9191

92-
/// ClickstreamAnalytics item attributes
92+
/// ClickstreamAnalytics preset item attributes
93+
/// In addition to the item attributes defined below, you can add up to 10 custom attributes to an item.
9394
public enum Item {
94-
static let ITEM_ID = "id"
95-
static let ITEM_NAME = "name"
96-
static let LOCATION_ID = "location_id"
97-
static let ITEM_BRAND = "brand"
98-
static let CURRENCY = "currency"
99-
static let PRICE = "price"
100-
static let QUANTITY = "quantity"
101-
static let CREATIVE_NAME = "creative_name"
102-
static let CREATIVE_SLOT = "creative_slot"
103-
static let ITEM_CATEGORY = "item_category"
104-
static let ITEM_CATEGORY2 = "item_category2"
105-
static let ITEM_CATEGORY3 = "item_category3"
106-
static let ITEM_CATEGORY4 = "item_category4"
107-
static let ITEM_CATEGORY5 = "item_category5"
95+
/// The id of the item
96+
public static let ITEM_ID = "id"
97+
/// The name of the item
98+
public static let ITEM_NAME = "name"
99+
/// The location id of the item
100+
public static let LOCATION_ID = "location_id"
101+
/// The brand of the item
102+
public static let ITEM_BRAND = "brand"
103+
/// The currency of the item
104+
public static let CURRENCY = "currency"
105+
/// The price of the item
106+
public static let PRICE = "price"
107+
/// The quantity of the item
108+
public static let QUANTITY = "quantity"
109+
/// The creative name of the item
110+
public static let CREATIVE_NAME = "creative_name"
111+
/// The creative slot of the item
112+
public static let CREATIVE_SLOT = "creative_slot"
113+
/// The category of the item
114+
public static let ITEM_CATEGORY = "item_category"
115+
/// The category2 of the item
116+
public static let ITEM_CATEGORY2 = "item_category2"
117+
/// The category3 of the item
118+
public static let ITEM_CATEGORY3 = "item_category3"
119+
/// The category4 of the item
120+
public static let ITEM_CATEGORY4 = "item_category4"
121+
/// The category5 of the item
122+
public static let ITEM_CATEGORY5 = "item_category5"
108123
}
109124
}

Sources/Clickstream/Dependency/Clickstream/Analytics/EventRecorder.swift

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@ class EventRecorder: AnalyticsEventRecording {
4343
/// save an clickstream event to storage
4444
/// - Parameter event: A ClickstreamEvent
4545
func save(_ event: ClickstreamEvent) throws {
46-
let eventJson: String = event.toJson()
46+
let eventObject = event.toJsonObject()
47+
let eventJson = eventObject.toJsonString()
4748
let eventSize = eventJson.count
4849
let storageEvent = StorageEvent(eventJson: eventJson, eventSize: Int64(eventSize))
4950
try dbUtil.saveEvent(storageEvent)
5051
if clickstream.configuration.isLogEvents {
5152
setLogLevel(logLevel: LogLevel.debug)
52-
logEventPrettier(event: event)
53+
log.debug("Saved event: \(event.eventType)\n\(eventObject.toPrettierJsonString())")
5354
}
5455
while try dbUtil.getTotalSize() > Constants.maxDbSize {
5556
let events = try dbUtil.getEventsWith(limit: 5)
@@ -144,15 +145,6 @@ class EventRecorder: AnalyticsEventRecording {
144145
}
145146
return BatchEvent(eventsJson: eventsJson, eventCount: eventCount, lastEventId: lastEventId)
146147
}
147-
148-
func logEventPrettier(event: ClickstreamEvent) {
149-
var attributesStr = "attributes:{\n"
150-
for (key, value) in event.attributes {
151-
attributesStr += " \"\(key)\": \(value)\n"
152-
}
153-
attributesStr += "}"
154-
log.debug("Event saved, event name: \(event.eventType)\n\(attributesStr)")
155-
}
156148
}
157149

158150
extension EventRecorder: ClickstreamLogger {}

Sources/Clickstream/Dependency/Clickstream/Event/ClickstreamEvent.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class ClickstreamEvent: AnalyticsPropertiesModel {
7979
attributes[key]
8080
}
8181

82-
func toJson() -> String {
82+
func toJsonObject() -> JsonObject {
8383
var event = JsonObject()
8484
event["unique_id"] = uniqueId
8585
event["event_type"] = eventType
@@ -111,7 +111,7 @@ class ClickstreamEvent: AnalyticsPropertiesModel {
111111
}
112112
event["user"] = userAttributes
113113
event["attributes"] = getAttributeObject(from: attributes)
114-
return event.toJsonString()
114+
return event
115115
}
116116

117117
private func getAttributeObject(from dictionary: AnalyticsProperties) -> JsonObject {
@@ -134,10 +134,6 @@ class ClickstreamEvent: AnalyticsPropertiesModel {
134134
}
135135
return attribute
136136
}
137-
138-
static func == (lhs: ClickstreamEvent, rhs: ClickstreamEvent) -> Bool {
139-
lhs.toJson() == rhs.toJson()
140-
}
141137
}
142138

143139
// MARK: - ClickstreamLogger

Sources/Clickstream/Support/Extension/JsonObject+ToJsonString.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,14 @@ extension JsonObject {
1717
}
1818
return ""
1919
}
20+
21+
func toPrettierJsonString() -> String {
22+
do {
23+
let jsonData = try JSONSerialization.data(withJSONObject: self, options: [.sortedKeys, .prettyPrinted])
24+
return String(data: jsonData, encoding: .utf8) ?? ""
25+
} catch {
26+
print("Error serializing dictionary to JSON: \(error.localizedDescription)")
27+
}
28+
return ""
29+
}
2030
}

Tests/ClickstreamTests/Clickstream/ClickstreamEventTest.swift

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,4 @@ class ClickstreamEventTest: XCTestCase {
153153
XCTAssertTrue(errorValueString.contains(ClickstreamAnalytics.Item.ITEM_NAME))
154154
XCTAssertEqual(0, clickstreamEvent.items.count)
155155
}
156-
157-
func testEventEqualsFail() {
158-
let event1 = clickstreamEvent!
159-
let event2 = ClickstreamEvent(eventType: "testEvent",
160-
appId: testAppId,
161-
uniqueId: UUID().uuidString,
162-
session: Session(uniqueId: UUID().uuidString, sessionIndex: 1),
163-
systemInfo: SystemInfo(storage: storage),
164-
netWorkType: NetWorkType.Wifi)
165-
XCTAssertFalse(event1 == event2)
166-
}
167156
}

Tests/ClickstreamTests/Clickstream/EventRecorderTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ class EventRecorderTest: XCTestCase {
416416

417417
func testVerifyHashCodeInRequestParameter() {
418418
clickstream.configuration.endpoint = testHashCodeEndpoint
419-
let eventJson = "[" + clickstreamEvent.toJson() + "]"
419+
let eventJson = "[" + clickstreamEvent.toJsonObject().toJsonString() + "]"
420420
let eventJsonHashCode = eventJson.hashCode()
421421
server["/collect/hashcode"] = { request in
422422
let queryParams = request.queryParams

Tests/ClickstreamTests/DataBase/ClickstreamDBUtilTest.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ class ClickstreamDBUtiltest: XCTestCase {
2929
session: Session(uniqueId: UUID().uuidString, sessionIndex: 1),
3030
systemInfo: SystemInfo(storage: storage),
3131
netWorkType: NetWorkType.Wifi)
32-
let eventJson = clickstreamEvent.toJson()
33-
storageEvent = StorageEvent(eventJson: clickstreamEvent.toJson(), eventSize: Int64(eventJson.count))
32+
let eventJson = clickstreamEvent.toJsonObject().toJsonString()
33+
storageEvent = StorageEvent(eventJson: clickstreamEvent.toJsonObject().toJsonString(), eventSize: Int64(eventJson.count))
3434
} catch {
3535
XCTFail("Fail to setup dbUtil error:\(error)")
3636
}

0 commit comments

Comments
 (0)