From b624f5aa1a52be8809a0b78fce3b0d3c0dc7412f Mon Sep 17 00:00:00 2001 From: jakemor Date: Mon, 13 Sep 2021 23:12:20 -0400 Subject: [PATCH] exposes locale to event parameters in pricing related events --- Sources/Paywall/Misc/Extensions.swift | 5 +++++ Sources/Paywall/Network/Device.swift | 2 +- Sources/Paywall/Paywall/Events.swift | 22 ++++++++++++++++++- .../Paywall/SWPaywallViewController.swift | 3 ++- 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/Sources/Paywall/Misc/Extensions.swift b/Sources/Paywall/Misc/Extensions.swift index abc12655f..2edeb67c8 100644 --- a/Sources/Paywall/Misc/Extensions.swift +++ b/Sources/Paywall/Misc/Extensions.swift @@ -71,6 +71,11 @@ internal extension SKProduct { "periodWeeks": periodWeeks, "periodMonths": periodMonths, "periodYears": periodYears, + "locale": priceLocale.identifier, + "languageCode": priceLocale.languageCode ?? "n/a", + "currencyCode": priceLocale.currencyCode ?? "n/a", + "currencySymbol": priceLocale.currencySymbol ?? "n/a", + ] } diff --git a/Sources/Paywall/Network/Device.swift b/Sources/Paywall/Network/Device.swift index 5ddfb423f..59f73a93b 100644 --- a/Sources/Paywall/Network/Device.swift +++ b/Sources/Paywall/Network/Device.swift @@ -34,7 +34,7 @@ internal class DeviceHelper { } var locale: String { - get { Locale.autoupdatingCurrent.identifier ?? "" } + get { Locale.autoupdatingCurrent.identifier } } var languageCode: String { diff --git a/Sources/Paywall/Paywall/Events.swift b/Sources/Paywall/Paywall/Events.swift index bf8cc4030..d723faafe 100644 --- a/Sources/Paywall/Paywall/Events.swift +++ b/Sources/Paywall/Paywall/Events.swift @@ -314,7 +314,7 @@ extension Paywall { output["product_id"] = p.productIdentifier for k in p.eventData.keys { if let v = p.eventData[k] { - output["product_\(k)"] = v + output["product_\(k.camelCaseToSnakeCase())"] = v } } } @@ -560,3 +560,23 @@ extension Paywall { struct SuperwallEventError: LocalizedError { var message: String } + + +extension String { + func camelCaseToSnakeCase() -> String { + let acronymPattern = "([A-Z]+)([A-Z][a-z]|[0-9])" + let fullWordsPattern = "([a-z])([A-Z]|[0-9])" + let digitsFirstPattern = "([0-9])([A-Z])" + return self.processCamelCaseRegex(pattern: acronymPattern)? + .processCamelCaseRegex(pattern: fullWordsPattern)? + .processCamelCaseRegex(pattern:digitsFirstPattern)?.lowercased() ?? self.lowercased() + } + + + + fileprivate func processCamelCaseRegex(pattern: String) -> String? { + let regex = try? NSRegularExpression(pattern: pattern, options: []) + let range = NSRange(location: 0, length: count) + return regex?.stringByReplacingMatches(in: self, options: [], range: range, withTemplate: "$1_$2") + } +} diff --git a/Sources/Paywall/Paywall/SWPaywallViewController.swift b/Sources/Paywall/Paywall/SWPaywallViewController.swift index c2997e8d7..7c6900f39 100644 --- a/Sources/Paywall/Paywall/SWPaywallViewController.swift +++ b/Sources/Paywall/Paywall/SWPaywallViewController.swift @@ -92,7 +92,7 @@ internal class SWPaywallViewController: UIViewController { self?.shimmerView.transform = .identity self?.purchaseLoadingIndicator.alpha = 0.0 self?.purchaseLoadingIndicator.transform = CGAffineTransform(scaleX: 0.05, y: 0.05) - }, completion: { [weak self] _ in + }, completion: { _ in // self?.purchaseLoadingIndicator.stopAnimating() }) case .ready: @@ -192,6 +192,7 @@ internal class SWPaywallViewController: UIViewController { wv.scrollView.minimumZoomScale = 1.0 wv.scrollView.backgroundColor = .clear wv.scrollView.isOpaque = false + return wv