-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MBL-1223] Track attribution event (#1961)
* Track attribution event * Return nil instead of force unwrapping
- Loading branch information
Showing
4 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import Foundation | ||
|
||
struct AttributionTracking { | ||
public enum AttributionEvent: String { | ||
case projectPageViewed = "Project Page Viewed" | ||
} | ||
|
||
static func eventParametersString(refInfo: RefInfo?) -> String? { | ||
let sessionRefTag = refInfo?.refTag?.stringTag | ||
let contextPageUrl = refInfo?.deeplinkUrl | ||
var props = [String: String]() | ||
if let sessionRefTag { | ||
props["session_ref_tag"] = sessionRefTag | ||
} | ||
props["context_page_url"] = contextPageUrl ?? "" | ||
|
||
guard let propsData = try? JSONSerialization.data(withJSONObject: props) else { | ||
return nil | ||
} | ||
return String(data: propsData, encoding: .utf8) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
@testable import Library | ||
import XCTest | ||
|
||
final class AttributionTrackingTests: TestCase { | ||
func testParametersString() { | ||
let ref = "test-tag" | ||
let url = "test-url" | ||
let refInfo = RefInfo(RefTag(code: ref), deeplinkUrl: url) | ||
|
||
let string = AttributionTracking.eventParametersString(refInfo: refInfo) | ||
let json = self.jsonDict(string!) | ||
|
||
XCTAssertEqual(json["session_ref_tag"], ref) | ||
XCTAssertEqual(json["context_page_url"], url) | ||
} | ||
|
||
func testParametersString_emptyRefInfo() { | ||
let refInfo = RefInfo(nil) | ||
|
||
let string = AttributionTracking.eventParametersString(refInfo: refInfo) | ||
let json = self.jsonDict(string!) | ||
|
||
XCTAssertEqual(json["context_page_url"], "") | ||
XCTAssertEqual(json.count, 1) // Json shouldn't contain any other fields. | ||
} | ||
|
||
// MARK: - Helpers | ||
|
||
private func jsonDict(_ jsonString: String) -> [String: String] { | ||
let data = jsonString.data(using: .utf8) | ||
return try! JSONDecoder().decode([String: String].self, from: data!) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters