Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions Example/BasicExample/BasicExample/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ struct ContentView: View {
VStack {
HStack {
Button(action: {
var traits = [String:Any]()
var traits = [String:Codable]()
var products = [Any]()
products.append(["price":1,"quantity":1,"productId":"foo","color":"blue", "dupe":"override"])
products.append(["price":2,"quantity":2,"productId":"bar","size":"large"])
products.append(["price":3,"quantity":3,"productId":"baz","fit":9])
traits["products"] = products
let productStr = convertIntoJSONString(arrayObject: products)
traits["products"] = productStr
traits["dupe"] = "default"
traits["general"] = "value"
Analytics.main.track(name: "Order Completed", properties: traits)
Expand All @@ -39,7 +40,7 @@ struct ContentView: View {
Text("Group")
}).padding(6)
Button(action: {
var traits = [String:Any]()
var traits = [String:Codable]()
traits["birthday"] = "1980-06-07T01:21:13Z"
traits["email"] = "[email protected]"
traits["firstName"] = "fnu"
Expand All @@ -61,6 +62,20 @@ struct ContentView: View {
print("Executed Analytics onDisappear()")
}
}

func convertIntoJSONString(arrayObject: [Any]) -> String? {

do {
let jsonData: Data = try JSONSerialization.data(withJSONObject: arrayObject, options: [])
if let jsonString = NSString(data: jsonData, encoding: String.Encoding.utf8.rawValue) {
return jsonString as String
}

} catch let error as NSError {
print("Array convertIntoJSON - \(error.description)")
}
return nil
}
}

struct ContentView_Previews: PreviewProvider {
Expand Down
43 changes: 43 additions & 0 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ let package = Package(
.package(
name: "Segment",
url: "https://github.com/segmentio/analytics-swift.git",
from: "1.1.2"
from: "1.4.1"
),
.package(
name: "braze-swift-sdk",
url:"https://github.com/braze-inc/braze-swift-sdk.git",
from: "5.0.0"),
from: "5.8.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand Down
28 changes: 7 additions & 21 deletions Sources/SegmentBraze/BrazeDestination.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ import UIKit
An implementation of the Braze Analytics device mode destination as a plugin.
*/

@objc(SEGBrazeDestination)
public class ObjCSegmentBraze: NSObject, ObjCDestination, ObjCDestinationShim {
public func instance() -> DestinationPlugin { return BrazeDestination() }
}

public class BrazeDestination: DestinationPlugin {
public let timeline = Timeline()
public let type = PluginType.destination
Expand All @@ -59,11 +64,11 @@ public class BrazeDestination: DestinationPlugin {
guard let tempSettings: BrazeSettings = settings.integrationSettings(forPlugin: self) else { return }
brazeSettings = tempSettings

var configuration = Braze.Configuration(
let configuration = Braze.Configuration(
apiKey: brazeSettings?.apiKey ?? "",
endpoint: brazeSettings?.customEndpoint ?? ""
)
configuration.api.addSdkMetadata([Braze.Configuration.Api.SdkMetadata.segment])
configuration.api.addSDKMetadata([Braze.Configuration.Api.SDKMetadata.segment])

braze = Braze(configuration: configuration)
}
Expand Down Expand Up @@ -232,7 +237,6 @@ private struct BrazeSettings: Codable {
let minimumIntervalBetweenTriggerActionsInSeconds: Double?
let allowCrawlerActivity: Bool?
let enableHtmlInAppMessages: Bool?
// let versionSettings: Dictionary<String, Any?> // not Codable
let bundlingStatus: String?
let serviceWorkerLocation: String?
let requireExplicitInAppMessageDismissal: Bool?
Expand Down Expand Up @@ -266,21 +270,3 @@ extension BrazeDestination {
}

}

// Rules for converting keys and values to the proper formats that bridge
// from Segment to the Partner SDK. These are only examples.
private extension BrazeDestination {

static var eventNameMap = ["ADD_TO_CART": "Product Added",
"PRODUCT_TAPPED": "Product Tapped"]

static var eventValueConversion: ((_ key: String, _ value: Any) -> Any) = { (key, value) in
if let valueString = value as? String {
return valueString
.replacingOccurrences(of: "-", with: "_")
.replacingOccurrences(of: " ", with: "_")
} else {
return value
}
}
}