forked from matomo-org/matomo-sdk-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ecommerce implementation (matomo-org#110)
- Loading branch information
Yury Krainik
committed
Aug 8, 2018
1 parent
5b88662
commit 6a54bf9
Showing
5 changed files
with
177 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,33 @@ | ||
// | ||
// EcommerceItem.swift | ||
// MatomoTracker | ||
// | ||
// Created by Yury Krainik on 8/8/18. | ||
// Copyright © 2018 Yury Krainik. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public struct EcommerceItem { | ||
let sku: String | ||
let name: String? | ||
let category: String? | ||
let price: Double | ||
let quantity: Double | ||
let formatter: NumberFormatter | ||
|
||
public func jsonObject() -> Any { | ||
|
||
var dict = ["sku": sku, "quantity": formatter.string(from: quantity as NSNumber), "price": formatter.string(from: price as NSNumber)] | ||
|
||
if let name = name { | ||
dict["name"] = name | ||
} | ||
|
||
if let category = category { | ||
dict["category"] = category | ||
} | ||
|
||
return dict | ||
} | ||
} |
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,53 @@ | ||
// | ||
// EcommerceOrder.swift | ||
// MatomoTracker | ||
// | ||
// Created by Yury Krainik on 8/8/18. | ||
// Copyright © 2018 Yury Krainik. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public class EcommerceOrder: EcommerceUpdate { | ||
public let orderId: String | ||
public var subtotal: Double? | ||
public var shipping: Double? | ||
public var discount: Double? | ||
public var tax: Double? | ||
|
||
init(orderId: String, formatter: NumberFormatter) { | ||
self.orderId = orderId | ||
super.init(formatter: formatter) | ||
} | ||
|
||
public override func jsonDict() -> [String : Any] { | ||
var dict = super.jsonDict() | ||
dict["id"] = orderId | ||
|
||
if let subtotal = subtotal { | ||
dict["ec_st"] = formatter.string(from: subtotal as NSNumber) | ||
} | ||
|
||
if let shipping = shipping { | ||
dict["ec_sh"] = formatter.string(from: shipping as NSNumber) | ||
} | ||
|
||
if let discount = discount { | ||
dict["ec_dt"] = formatter.string(from: discount as NSNumber) | ||
} | ||
|
||
if let tax = tax { | ||
dict["ec_tx"] = formatter.string(from: tax as NSNumber) | ||
} | ||
|
||
return dict | ||
} | ||
|
||
public override func clearAll() { | ||
super.clearAll() | ||
subtotal = nil | ||
shipping = nil | ||
discount = nil | ||
tax = nil | ||
} | ||
} |
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,54 @@ | ||
// | ||
// EcommerceUpdate.swift | ||
// MatomoTracker | ||
// | ||
// Created by Yury Krainik on 8/8/18. | ||
// Copyright © 2018 Matomo. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public class EcommerceUpdate { | ||
public var items: [String: EcommerceItem] = [:] | ||
public var revenue: Double = 0 | ||
public let formatter: NumberFormatter | ||
|
||
init(formatter: NumberFormatter) { | ||
self.formatter = formatter | ||
} | ||
|
||
public func jsonDict() -> [String: Any] { | ||
|
||
let jsonItems = items.map { item -> Any in | ||
item.value.jsonObject() | ||
} | ||
|
||
return ["idgoal": "0", "ec_items": jsonItems, "revenue": formatter.string(from: revenue as NSNumber) ?? "0"] | ||
} | ||
|
||
public func variables() throws -> [CustomVariable] { | ||
|
||
return try jsonDict().enumerated().map { (arg) -> CustomVariable in | ||
|
||
let (offset, element) = arg | ||
|
||
let data = try JSONSerialization.data(withJSONObject: element.value, options: []) | ||
let jsonValue = String.init(data: data, encoding: String.Encoding.utf8) ?? "" | ||
|
||
return CustomVariable(index: UInt(offset), name: element.key, value: jsonValue) | ||
} | ||
} | ||
|
||
public func addItem(_ item: EcommerceItem) { | ||
items[item.sku] = item | ||
} | ||
|
||
public func removeItem(_ item: EcommerceItem) { | ||
items.removeValue(forKey: item.sku) | ||
} | ||
|
||
public func clearAll() { | ||
items.removeAll() | ||
revenue = 0 | ||
} | ||
} |
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