Skip to content

Commit

Permalink
feat: add preset traffic source attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoweii committed Apr 10, 2024
1 parent a2c331e commit 712a4a6
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 4 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ func application(_ application: UIApplication, didFinishLaunchingWithOptions lau

#### 3.2 Initialize the SDK with global attributes and custom configuration

The following example code shows how to add traffic source fields as global attributes when initializing the SDK.

```swift
import Clickstream
...
Expand All @@ -92,8 +94,15 @@ func application(_ application: UIApplication, didFinishLaunchingWithOptions lau
.withEndpoint("https://example.com/collect")
.withLogEvents(true)
.withInitialGlobalAttributes([
"_traffic_source_name": "Summer promotion",
"_traffic_source_medium": "Search engine"
ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_SOURCE: "amazon",
ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_MEDIUM: "cpc",
ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_CAMPAIGN: "summer_promotion",
ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_CAMPAIGN_ID: "summer_promotion_01",
ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_TERM: "running_shoes",
ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_CONTENT: "banner_ad_1",
ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_CLID: "amazon_ad_123",
ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_CLID_PLATFORM: "amazon_ads",
ClickstreamAnalytics.Attr.APP_INSTALL_CHANNEL: "App Store"
])
try ClickstreamAnalytics.initSDK(configuration)
} catch {
Expand Down
24 changes: 24 additions & 0 deletions Sources/Clickstream/ClickstreamAnalytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,32 @@ public enum ClickstreamAnalytics {

/// ClickstreamANalytics preset attributes
public enum Attr {
/// Preset attribute screen name
public static let SCREEN_NAME = "_screen_name"
/// Preset attribute screen unique id
public static let SCREEN_UNIQUE_ID = "_screen_unique_id"
/// Preset attribute traffic source source
public static let TRAFFIC_SOURCE_SOURCE = "_traffic_source_source"
/// Preset attribute traffic source medium
public static let TRAFFIC_SOURCE_MEDIUM = "_traffic_source_medium"
/// Preset attribute traffic source campaign
public static let TRAFFIC_SOURCE_CAMPAIGN = "_traffic_source_campaign"
/// Preset attribute traffic source campaign id
public static let TRAFFIC_SOURCE_CAMPAIGN_ID = "_traffic_source_campaign_id"
/// Preset attribute traffic source term
public static let TRAFFIC_SOURCE_TERM = "_traffic_source_term"
/// Preset attribute traffic source content
public static let TRAFFIC_SOURCE_CONTENT = "_traffic_source_content"
/// Preset attribute traffic source clid
public static let TRAFFIC_SOURCE_CLID = "_traffic_source_clid"
/// Preset attribute traffic source clid platform
public static let TRAFFIC_SOURCE_CLID_PLATFORM = "_traffic_source_clid_platform"
/// Preset attribute app install channel
public static let APP_INSTALL_CHANNEL = "_app_install_channel"
/// Preset attribute event value
public static let VALUE = "_value"
/// Preset attribute event currency
public static let CURRENCY = "_currency"
}

/// ClickstreamAnalytics preset item attributes
Expand Down
22 changes: 22 additions & 0 deletions Sources/Clickstream/ClickstreamObjc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,28 @@ import Foundation
public static let SCREEN_NAME = "_screen_name"
/// Preset attribute screen unique id
public static let SCREEN_UNIQUE_ID = "_screen_unique_id"
/// Preset attribute traffic source source
public static let TRAFFIC_SOURCE_SOURCE = "_traffic_source_source"
/// Preset attribute traffic source medium
public static let TRAFFIC_SOURCE_MEDIUM = "_traffic_source_medium"
/// Preset attribute traffic source campaign
public static let TRAFFIC_SOURCE_CAMPAIGN = "_traffic_source_campaign"
/// Preset attribute traffic source campaign id
public static let TRAFFIC_SOURCE_CAMPAIGN_ID = "_traffic_source_campaign_id"
/// Preset attribute traffic source term
public static let TRAFFIC_SOURCE_TERM = "_traffic_source_term"
/// Preset attribute traffic source content
public static let TRAFFIC_SOURCE_CONTENT = "_traffic_source_content"
/// Preset attribute traffic source clid
public static let TRAFFIC_SOURCE_CLID = "_traffic_source_clid"
/// Preset attribute traffic source clid platform
public static let TRAFFIC_SOURCE_CLID_PLATFORM = "_traffic_source_clid_platform"
/// Preset attribute app install channel
public static let APP_INSTALL_CHANNEL = "_app_install_channel"
/// Preset attribute event value
public static let VALUE = "_value"
/// Preset attribute event currency
public static let CURRENCY = "_currency"
}

/// ClickstreamAnalytics preset item keys for objective-c
Expand Down
65 changes: 63 additions & 2 deletions Tests/ClickstreamTests/IntegrationTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ class IntegrationTest: XCTestCase {
ClickstreamAnalytics.Item.ITEM_CATEGORY: "book",
ClickstreamAnalytics.Item.PRICE: 99.9
]
ClickstreamAnalytics.recordEvent("testEvent", ["id": 123], [item_book])
ClickstreamAnalytics.recordEvent("testEvent",
["id": 123,
ClickstreamAnalytics.Attr.VALUE: 99.9,
ClickstreamAnalytics.Attr.CURRENCY: "USD"],
[item_book])
Thread.sleep(forTimeInterval: 0.2)
let testEvent = try getTestEvent()
let items = testEvent["items"] as! [JsonObject]
Expand Down Expand Up @@ -136,6 +140,33 @@ class IntegrationTest: XCTestCase {
XCTAssertEqual(true, eventAttribute["isOpenNotification"] as! Bool)
}

func testAddGlobalAttributeForTrafficSource() throws {
ClickstreamAnalytics.addGlobalAttributes([
ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_SOURCE: "amazon",
ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_MEDIUM: "cpc",
ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_CAMPAIGN: "summer_promotion",
ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_CAMPAIGN_ID: "summer_promotion_01",
ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_TERM: "running_shoes",
ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_CONTENT: "banner_ad_1",
ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_CLID: "amazon_ad_123",
ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_CLID_PLATFORM: "amazon_ads",
ClickstreamAnalytics.Attr.APP_INSTALL_CHANNEL: "App Store"
])
ClickstreamAnalytics.recordEvent("testEvent")
Thread.sleep(forTimeInterval: 0.1)
let testEvent = try getTestEvent()
let eventAttribute = testEvent["attributes"] as! [String: Any]
XCTAssertEqual("amazon", eventAttribute[ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_SOURCE] as! String)
XCTAssertEqual("cpc", eventAttribute[ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_MEDIUM] as! String)
XCTAssertEqual("summer_promotion", eventAttribute[ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_CAMPAIGN] as! String)
XCTAssertEqual("summer_promotion_01", eventAttribute[ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_CAMPAIGN_ID] as! String)
XCTAssertEqual("running_shoes", eventAttribute[ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_TERM] as! String)
XCTAssertEqual("banner_ad_1", eventAttribute[ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_CONTENT] as! String)
XCTAssertEqual("amazon_ad_123", eventAttribute[ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_CLID] as! String)
XCTAssertEqual("amazon_ads", eventAttribute[ClickstreamAnalytics.Attr.TRAFFIC_SOURCE_CLID_PLATFORM] as! String)
XCTAssertEqual("App Store", eventAttribute[ClickstreamAnalytics.Attr.APP_INSTALL_CHANNEL] as! String)
}

func testDeleteGlobalAttribute() throws {
ClickstreamAnalytics.addGlobalAttributes([
"channel": "AppStore",
Expand Down Expand Up @@ -326,7 +357,9 @@ class IntegrationTest: XCTestCase {
"event_category": "recommended"
]
ClickstreamObjc.recordEvent("testEvent",
["id": 123],
["id": 123,
Attr.VALUE: 99.9,
Attr.CURRENCY: "USD"],
[item])
Thread.sleep(forTimeInterval: 0.2)
let testEvent = try getTestEvent()
Expand Down Expand Up @@ -371,6 +404,34 @@ class IntegrationTest: XCTestCase {
XCTAssertEqual(true, eventAttribute["Successful"] as! Bool)
}

func testAddTrafficSourceForObjc() throws {
let attribute: NSDictionary = [
Attr.TRAFFIC_SOURCE_SOURCE: "amazon",
Attr.TRAFFIC_SOURCE_MEDIUM: "cpc",
Attr.TRAFFIC_SOURCE_CAMPAIGN: "summer_promotion",
Attr.TRAFFIC_SOURCE_CAMPAIGN_ID: "summer_promotion_01",
Attr.TRAFFIC_SOURCE_TERM: "running_shoes",
Attr.TRAFFIC_SOURCE_CONTENT: "banner_ad_1",
Attr.TRAFFIC_SOURCE_CLID: "amazon_ad_123",
Attr.TRAFFIC_SOURCE_CLID_PLATFORM: "amazon_ads",
Attr.APP_INSTALL_CHANNEL: "App Store"
]
ClickstreamObjc.addGlobalAttributes(attribute)
ClickstreamObjc.recordEvent("testEvent")
Thread.sleep(forTimeInterval: 0.1)
let testEvent = try getTestEvent()
let eventAttribute = testEvent["attributes"] as! [String: Any]
XCTAssertNotNil(eventAttribute[Attr.TRAFFIC_SOURCE_SOURCE])
XCTAssertNotNil(eventAttribute[Attr.TRAFFIC_SOURCE_MEDIUM])
XCTAssertNotNil(eventAttribute[Attr.TRAFFIC_SOURCE_CAMPAIGN])
XCTAssertNotNil(eventAttribute[Attr.TRAFFIC_SOURCE_CAMPAIGN_ID])
XCTAssertNotNil(eventAttribute[Attr.TRAFFIC_SOURCE_TERM])
XCTAssertNotNil(eventAttribute[Attr.TRAFFIC_SOURCE_CONTENT])
XCTAssertNotNil(eventAttribute[Attr.TRAFFIC_SOURCE_CLID])
XCTAssertNotNil(eventAttribute[Attr.TRAFFIC_SOURCE_CLID_PLATFORM])
XCTAssertNotNil(eventAttribute[Attr.APP_INSTALL_CHANNEL])
}

func testUserAttributeForObjc() throws {
ClickstreamObjc.setUserId("3231")
let userAttribute: NSDictionary = [
Expand Down

0 comments on commit 712a4a6

Please sign in to comment.