Skip to content

Commit

Permalink
[TP 147318] - fix crash that occurs in the test app (#37)
Browse files Browse the repository at this point in the history
* ensure api key is set before attempting to track an event
  • Loading branch information
ndurell authored Oct 26, 2022
1 parent 27601dc commit 62667b8
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 17 deletions.
27 changes: 15 additions & 12 deletions Sources/KlaviyoSwift/Klaviyo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,15 @@ public class Klaviyo : NSObject {
let customerPropertiesDict = updatePropertiesDictionary(propDictionary: customerProperties)
assertPropertyTypes(properties: propertiesDict)

guard let apiKey = apiKey else {
environment.logger.error("Track event called before API key was set.")
//TODO: store pending event for when api key is set.
return
}

serialQueue.async(execute: {
let event = NSMutableDictionary()

// Set the apiKey for the event
if (self.apiKey!.count > 0) {
event[self.KLEventTrackTokenJSONKey] = self.apiKey
} else {
event[self.KLEventTrackTokenJSONKey] = ""
}
event[self.KLEventTrackTokenJSONKey] = apiKey

// If it's a push event, set a service key to Klaviyo
var service: String = "api"
Expand Down Expand Up @@ -292,18 +292,21 @@ public class Klaviyo : NSObject {
if personDictionary.allKeys.count == 0 {
return
}

guard let apiKey = apiKey else {
environment.logger.error("Track person called before API key was set.")
//TODO: store pending data for when api key is set.
return
}

// Update properties for JSON encoding
let personInfoDictionary = updatePropertiesDictionary(propDictionary: personDictionary)
assertPropertyTypes(properties: personInfoDictionary)

serialQueue.async(execute: {
let event = NSMutableDictionary()

if self.apiKey!.count > 0 {
event[self.KLPersonTrackTokenJSONKey] = self.apiKey
} else {
event[self.KLPersonTrackTokenJSONKey] = ""
}
event[self.KLPersonTrackTokenJSONKey] = apiKey

event[self.KLPersonPropertiesJSONKey] = personInfoDictionary
self.peopleQueue!.add(_: event)
Expand Down
4 changes: 3 additions & 1 deletion Sources/KlaviyoSwift/KlaviyoEnvironment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ struct KlaviyoEnvironment {
var archiverClient: ArchiverClient
var fileClient: FileClient
var data: (URL) throws -> Data
var logger: LoggerClient
static let production = KlaviyoEnvironment(
archiverClient: ArchiverClient.production,
fileClient: FileClient.production,
data: { url in try Data(contentsOf: url) }
data: { url in try Data(contentsOf: url) },
logger: LoggerClient.production
)

}
14 changes: 14 additions & 0 deletions Sources/KlaviyoSwift/LoggerClient.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// LoggerClient.swift
// KlaviyoSwift
//
// Created by Noah Durell on 10/21/22.
//

import Foundation
import os

struct LoggerClient {
var error: (String) -> ()
static let production = Self(error: { message in os_log("%{public}@", type: .error) })
}
11 changes: 10 additions & 1 deletion Tests/KlaviyoSwiftTests/KlaviyoTestUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ extension ArchiverClient {

extension KlaviyoEnvironment {
static var testURL = { (_:String) in TEST_URL }
static var lastLog: String?
static let test = KlaviyoEnvironment(
archiverClient: ArchiverClient.test,
fileClient: FileClient.test,
data: { _ in TEST_RETURN_DATA }
data: { _ in TEST_RETURN_DATA },
logger: LoggerClient.test
)
}

Expand All @@ -46,3 +48,10 @@ extension FileClient {
)
}

extension LoggerClient {
static var lastLoggedMessage: String?
static let test = LoggerClient { message in
lastLoggedMessage = message
}
}

22 changes: 22 additions & 0 deletions Tests/KlaviyoSwiftTests/Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ class Tests: XCTestCase {
super.setUp()
Klaviyo.setupWithPublicAPIKey(apiKey: "wbg7GT")
klaviyo = Klaviyo.sharedInstance
environment = KlaviyoEnvironment.test
}

override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
LoggerClient.lastLoggedMessage = nil
}

func testAPIKeyExists() {
Expand Down Expand Up @@ -90,4 +92,24 @@ class Tests: XCTestCase {
}
}

func testTrackEventWithNoApiKey() {
klaviyo.apiKey = nil

klaviyo.trackEvent(eventName: "foo")

XCTAssertEqual(LoggerClient.lastLoggedMessage, "Track event called before API was set.")

}

func testTrackPersonWithNoApiKey() {
klaviyo.apiKey = nil

let emailDict: NSMutableDictionary = [klaviyo.KLPersonEmailDictKey: "[email protected]"]

klaviyo.trackPersonWithInfo(personDictionary: emailDict)

XCTAssertEqual(LoggerClient.lastLoggedMessage, "Track person called before setting the API Key.")

}

}
9 changes: 6 additions & 3 deletions klaviyo-swift-sdk.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

/* Begin PBXBuildFile section */
7C375BC028E77149008605B3 /* KlaviyoTestUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C375BBF28E77149008605B3 /* KlaviyoTestUtils.swift */; };
7C54A46F2902F01E00611E4E /* LoggerClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C54A46E2902F01E00611E4E /* LoggerClient.swift */; };
OBJ_33 /* ArchivalUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_9 /* ArchivalUtils.swift */; };
OBJ_34 /* FileUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_10 /* FileUtils.swift */; };
OBJ_35 /* Klaviyo.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_11 /* Klaviyo.swift */; };
Expand Down Expand Up @@ -53,6 +54,7 @@

/* Begin PBXFileReference section */
7C375BBF28E77149008605B3 /* KlaviyoTestUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KlaviyoTestUtils.swift; sourceTree = "<group>"; };
7C54A46E2902F01E00611E4E /* LoggerClient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoggerClient.swift; sourceTree = "<group>"; };
OBJ_10 /* FileUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileUtils.swift; sourceTree = "<group>"; };
OBJ_11 /* Klaviyo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Klaviyo.swift; sourceTree = "<group>"; };
OBJ_12 /* KlaviyoEnvironment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KlaviyoEnvironment.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -120,7 +122,7 @@
name = Products;
sourceTree = BUILT_PRODUCTS_DIR;
};
OBJ_5 /* */ = {
OBJ_5 = {
isa = PBXGroup;
children = (
OBJ_6 /* Package.swift */,
Expand All @@ -134,7 +136,6 @@
OBJ_26 /* Gemfile */,
OBJ_27 /* Gemfile.lock */,
);
name = "";
sourceTree = "<group>";
};
OBJ_7 /* Sources */ = {
Expand All @@ -153,6 +154,7 @@
OBJ_11 /* Klaviyo.swift */,
OBJ_12 /* KlaviyoEnvironment.swift */,
OBJ_13 /* ReachabilitySwift.swift */,
7C54A46E2902F01E00611E4E /* LoggerClient.swift */,
);
name = KlaviyoSwift;
path = Sources/KlaviyoSwift;
Expand Down Expand Up @@ -224,7 +226,7 @@
knownRegions = (
en,
);
mainGroup = OBJ_5 /* */;
mainGroup = OBJ_5;
productRefGroup = OBJ_19 /* Products */;
projectDirPath = "";
projectRoot = "";
Expand All @@ -246,6 +248,7 @@
OBJ_34 /* FileUtils.swift in Sources */,
OBJ_35 /* Klaviyo.swift in Sources */,
OBJ_36 /* KlaviyoEnvironment.swift in Sources */,
7C54A46F2902F01E00611E4E /* LoggerClient.swift in Sources */,
OBJ_37 /* ReachabilitySwift.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down

0 comments on commit 62667b8

Please sign in to comment.