Skip to content

Commit

Permalink
Merge pull request #4 from Tealium/2.0.0
Browse files Browse the repository at this point in the history
2.0.0
  • Loading branch information
jonathanswong authored Oct 21, 2020
2 parents 7b88e80 + a16e1da commit 2456156
Show file tree
Hide file tree
Showing 29 changed files with 323 additions and 899 deletions.
2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github "tealium/tealium-swift"
github "tealium/tealium-swift" ~> 2.1
github "ContentSquare/CS_iOS_SDK"
3 changes: 0 additions & 3 deletions Cartfile.resolved

This file was deleted.

79 changes: 0 additions & 79 deletions Objective-C/ContentsquareCommand.swift

This file was deleted.

42 changes: 0 additions & 42 deletions Objective-C/ContentsquareConstants.swift

This file was deleted.

24 changes: 0 additions & 24 deletions Objective-C/ContentsquareTrackable.swift

This file was deleted.

30 changes: 16 additions & 14 deletions Sources/ContentsquareConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,34 @@
// TealiumContentsquare
//
// Created by Jonathan Wong on 3/4/20.
// Copyright © 2020 Jonathan Wong. All rights reserved.
// Copyright © 2020 Tealium. All rights reserved.
//

extension ContentsquareRemoteCommand {
enum Contentsquare {

enum Commands {
static let commandKey = "command_name"
static let separator: Character = ","

static let sendScreenView = "sendscreenview"
static let sendTransaction = "sendtransaction"
static let sendDynamicVar = "senddynamicvar"
static let stopTracking = "stoptracking"
static let resumeTracking = "resumetracking"
static let forgetMe = "forgetme"
static let optIn = "optin"
static let optOut = "optout"

static let commandId = "contentsquare"
static let description = "Contentsquare Remote Command"
static let commandKey = "command_name"
static let separator: Character = ","

enum Commands: String {
case sendScreenView = "sendscreenview"
case sendTransaction = "sendtransaction"
case sendDynamicVar = "senddynamicvar"
case stopTracking = "stoptracking"
case resumeTracking = "resumetracking"
case forgetMe = "forgetme"
case optIn = "optin"
case optOut = "optout"
}

enum ScreenView {
static let screenName = "screen_name"
}

enum TransactionProperties {
static let purchase = "purchase"
static let transaction = "transaction"
static let price = "price" // required
static let currency = "currency" // required
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
//
// ContentsquareTracker.swift
// ContentsquareInstance.swift
// TealiumContentsquare
//
// Created by Jonathan Wong on 3/6/20.
// Copyright © 2020 Jonathan Wong. All rights reserved.
// Copyright © 2020 Tealium. All rights reserved.
//

import Foundation
import ContentsquareModule

public class ContentsquareTracker: ContentsquareTrackable {
public protocol ContentsquareCommand {
func sendScreenView(screenName: String)
func sendTransaction(price: Double, currency: String, transactionId: String?)
func sendDynamicVar(dynamicVar: [String: Any])
func stopTracking()
func resumeTracking()
func forgetMe()
func optIn()
func optOut()
}

public class ContentsquareInstance: ContentsquareCommand {

public init() {}

Expand All @@ -29,19 +40,11 @@ public class ContentsquareTracker: ContentsquareTrackable {
public func sendDynamicVar(dynamicVar: [String: Any]) {
dynamicVar.forEach { key, value in
if let value = value as? String {
do {
let dynamicVar = try DynamicVar(key: key, value: value)
Contentsquare.send(dynamicVar: dynamicVar)
} catch {
print("Error with dynamic variable key: \(key), value: \(value), error: \(error)")
}
let dynamicVar = DynamicVar(key: key, value: value)
Contentsquare.send(dynamicVar: dynamicVar)
} else if let value = value as? UInt32 {
do {
let dynamicVar = try DynamicVar(key: key, value: value)
Contentsquare.send(dynamicVar: dynamicVar)
} catch {
print("Error with dynamic variable key: \(key), value: \(value), error: \(error)")
}
let dynamicVar = DynamicVar(key: key, value: value)
Contentsquare.send(dynamicVar: dynamicVar)
} else {
print("Incorrect format of value: \(value). Value should be String or UInt32.")
}
Expand Down
96 changes: 49 additions & 47 deletions Sources/ContentsquareRemoteCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// TealiumContentsquare
//
// Created by Jonathan Wong on 3/6/20.
// Copyright © 2020 Jonathan Wong. All rights reserved.
// Copyright © 2020 Tealium. All rights reserved.
//

import Foundation
Expand All @@ -15,63 +15,65 @@ import TealiumTagManagement
import TealiumRemoteCommands
#endif

public class ContentsquareRemoteCommand {
public class ContentsquareRemoteCommand: RemoteCommand {

let contentsquareTracker:
ContentsquareTrackable
var contentsquareInstance: ContentsquareCommand?

public init(contentsquareTracker:
ContentsquareTrackable) {
self.contentsquareTracker =
contentsquareTracker
public init(contentsquareInstance: ContentsquareCommand = ContentsquareInstance(), type: RemoteCommandType = .webview) {
self.contentsquareInstance = contentsquareInstance
weak var weakSelf: ContentsquareRemoteCommand?
super.init(commandId: Contentsquare.commandId,
description: Contentsquare.description,
type: type,
completion: { response in
guard let payload = response.payload else {
return
}
weakSelf?.processRemoteCommand(with: payload)
})
weakSelf = self
}

public func remoteCommand() -> TealiumRemoteCommand {
return TealiumRemoteCommand(commandId: "contentsquare",
description: "Contentsquare Remote Command") { response in
let payload = response.payload()
guard let command = payload[Contentsquare.Commands.commandKey] as? String else {
return
}

let commands = command.split(separator: Contentsquare.Commands.separator)
let formatted = commands.map { command in
return command.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
}

self.parseCommands(formatted, payload: payload)
func processRemoteCommand(with payload: [String: Any]) {
guard let contentsquareInstance = contentsquareInstance,
let command = payload[Contentsquare.commandKey] as? String else {
return
}
}

func parseCommands(_ commands: [String], payload: [String: Any]) {
commands.forEach { command in
let lowercasedCommand = command.lowercased()
switch lowercasedCommand {
case Contentsquare.Commands.sendScreenView:
let commands = command.split(separator: Contentsquare.separator)
let contentsquareCommands = commands.map { command in
return command.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
}

contentsquareCommands.forEach {
let command = Contentsquare.Commands(rawValue: $0.lowercased())
switch command {
case .sendScreenView:
guard let screenName = payload[Contentsquare.ScreenView.screenName] as? String else { return }
contentsquareTracker.sendScreenView(screenName: screenName)
case Contentsquare.Commands.sendTransaction:
guard let options = payload[Contentsquare.TransactionProperties.transaction] as? [String: Any] else {
print("Contentsquare.TransactionProperties.transaction key is missing.")
return
contentsquareInstance.sendScreenView(screenName: screenName)
case .sendTransaction:
var options = [String: Any]()
if let transaction = payload[Contentsquare.TransactionProperties.transaction] as? [String: Any] {
options = transaction
} else if let purchase = payload[Contentsquare.TransactionProperties.purchase] as? [String: Any] {
options = purchase
}
guard let price: Double = options[Contentsquare.TransactionProperties.price] as? Double,
let currency: String = options[Contentsquare.TransactionProperties.currency] as? String else { return }
let transactionId: String? = options[Contentsquare.TransactionProperties.transactionId] as? String
contentsquareTracker.sendTransaction(price: price, currency: currency, transactionId: transactionId)
case Contentsquare.Commands.sendDynamicVar:
contentsquareInstance.sendTransaction(price: price, currency: currency, transactionId: transactionId)
case .sendDynamicVar:
guard let dynamicVar = payload[Contentsquare.DynamicVar.dynamicVar] as? [String: Any] else { return }
contentsquareTracker.sendDynamicVar(dynamicVar: dynamicVar)
case Contentsquare.Commands.stopTracking:
contentsquareTracker.stopTracking()
case Contentsquare.Commands.resumeTracking:
contentsquareTracker.resumeTracking()
case Contentsquare.Commands.forgetMe:
contentsquareTracker.forgetMe()
case Contentsquare.Commands.optIn:
contentsquareTracker.optIn()
case Contentsquare.Commands.optOut:
contentsquareTracker.optOut()
contentsquareInstance.sendDynamicVar(dynamicVar: dynamicVar)
case .stopTracking:
contentsquareInstance.stopTracking()
case .resumeTracking:
contentsquareInstance.resumeTracking()
case .forgetMe:
contentsquareInstance.forgetMe()
case .optIn:
contentsquareInstance.optIn()
case .optOut:
contentsquareInstance.optOut()
default: break
}
}
Expand Down
Loading

0 comments on commit 2456156

Please sign in to comment.