Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: logger uses updated message and modal properties #21

Merged
merged 16 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 15 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
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ concurrency:
jobs:
tests:
name: Tests
runs-on: macos-13
runs-on: macos-13-xlarge
steps:
- name: Checkout repository
uses: actions/checkout@v3
Expand Down
77 changes: 77 additions & 0 deletions Demo/Demo.xcodeproj/xcshareddata/xcschemes/Demo.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1510"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "C6E7C48D2A38AC56003C05E4"
BuildableName = "Demo.app"
BlueprintName = "Demo"
ReferencedContainer = "container:Demo.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "C6E7C48D2A38AC56003C05E4"
BuildableName = "Demo.app"
BlueprintName = "Demo"
ReferencedContainer = "container:Demo.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "C6E7C48D2A38AC56003C05E4"
BuildableName = "Demo.app"
BlueprintName = "Demo"
ReferencedContainer = "container:Demo.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
1 change: 0 additions & 1 deletion Demo/Demo/UIKitContentViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -362,4 +362,3 @@ extension UIKitContentViewController: PayPalMessageViewEventDelegate {
statusTextView.text = "Applied"
}
}
// swiftlint:disable:this file_length
36 changes: 20 additions & 16 deletions PayPalMessages.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
objects = {

/* Begin PBXBuildFile section */
C60F156A2B3C851100E16902 /* AnalyticsLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = C60F15692B3C851100E16902 /* AnalyticsLogger.swift */; };
C61D84472B33348C00F372EF /* CloudEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = C61D84462B33348C00F372EF /* CloudEvent.swift */; };
C635F4362A9645B10096F9FF /* PayPalMessages.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C635F42E2A9645B10096F9FF /* PayPalMessages.framework */; };
C635F4C12A964A020096F9FF /* PayPalMessageModalViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = C635F48E2A964A020096F9FF /* PayPalMessageModalViewModel.swift */; };
C635F4C22A964A020096F9FF /* ComponentLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = C635F4902A964A020096F9FF /* ComponentLogger.swift */; };
C635F4C32A964A020096F9FF /* ComponentLoggerEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = C635F4912A964A020096F9FF /* ComponentLoggerEvent.swift */; };
C635F4C42A964A020096F9FF /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = C635F4922A964A020096F9FF /* Logger.swift */; };
C635F4C32A964A020096F9FF /* AnalyticsEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = C635F4912A964A020096F9FF /* AnalyticsEvent.swift */; };
C635F4C42A964A020096F9FF /* AnalyticsService.swift in Sources */ = {isa = PBXBuildFile; fileRef = C635F4922A964A020096F9FF /* AnalyticsService.swift */; };
C635F4C52A964A020096F9FF /* AnyStringKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = C635F4932A964A020096F9FF /* AnyStringKey.swift */; };
C635F4C62A964A020096F9FF /* AnyCodable.swift in Sources */ = {isa = PBXBuildFile; fileRef = C635F4942A964A020096F9FF /* AnyCodable.swift */; };
C635F4C72A964A020096F9FF /* PayPalMessageConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = C635F4962A964A020096F9FF /* PayPalMessageConfig.swift */; };
Expand Down Expand Up @@ -84,12 +85,13 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
C60F15692B3C851100E16902 /* AnalyticsLogger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AnalyticsLogger.swift; sourceTree = "<group>"; };
C61D84462B33348C00F372EF /* CloudEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CloudEvent.swift; sourceTree = "<group>"; };
C635F42E2A9645B10096F9FF /* PayPalMessages.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = PayPalMessages.framework; sourceTree = BUILT_PRODUCTS_DIR; };
C635F4352A9645B10096F9FF /* PayPalMessagesTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = PayPalMessagesTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
C635F48E2A964A020096F9FF /* PayPalMessageModalViewModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PayPalMessageModalViewModel.swift; sourceTree = "<group>"; };
C635F4902A964A020096F9FF /* ComponentLogger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ComponentLogger.swift; sourceTree = "<group>"; };
C635F4912A964A020096F9FF /* ComponentLoggerEvent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ComponentLoggerEvent.swift; sourceTree = "<group>"; };
C635F4922A964A020096F9FF /* Logger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = "<group>"; };
C635F4912A964A020096F9FF /* AnalyticsEvent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AnalyticsEvent.swift; sourceTree = "<group>"; };
C635F4922A964A020096F9FF /* AnalyticsService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AnalyticsService.swift; sourceTree = "<group>"; };
C635F4932A964A020096F9FF /* AnyStringKey.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AnyStringKey.swift; sourceTree = "<group>"; };
C635F4942A964A020096F9FF /* AnyCodable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AnyCodable.swift; sourceTree = "<group>"; };
C635F4962A964A020096F9FF /* PayPalMessageConfig.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PayPalMessageConfig.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -209,7 +211,7 @@
C635F4BB2A964A020096F9FF /* Delegates */,
C635F49A2A964A020096F9FF /* Enums */,
C635F4B32A964A020096F9FF /* Extensions */,
C635F48F2A964A020096F9FF /* Logger */,
C635F48F2A964A020096F9FF /* Analytics */,
C635F4A52A964A020096F9FF /* IO */,
C635F4AC2A964A020096F9FF /* Utils */,
C635F4BE2A964A020096F9FF /* Views */,
Expand All @@ -222,16 +224,17 @@
path = PayPalMessages;
sourceTree = "<group>";
};
C635F48F2A964A020096F9FF /* Logger */ = {
C635F48F2A964A020096F9FF /* Analytics */ = {
isa = PBXGroup;
children = (
C635F4902A964A020096F9FF /* ComponentLogger.swift */,
C635F4912A964A020096F9FF /* ComponentLoggerEvent.swift */,
C635F4922A964A020096F9FF /* Logger.swift */,
C60F15692B3C851100E16902 /* AnalyticsLogger.swift */,
C635F4912A964A020096F9FF /* AnalyticsEvent.swift */,
C635F4922A964A020096F9FF /* AnalyticsService.swift */,
C635F4932A964A020096F9FF /* AnyStringKey.swift */,
C635F4942A964A020096F9FF /* AnyCodable.swift */,
C61D84462B33348C00F372EF /* CloudEvent.swift */,
);
path = Logger;
path = Analytics;
sourceTree = "<group>";
};
C635F4952A964A020096F9FF /* Config */ = {
Expand Down Expand Up @@ -267,7 +270,6 @@
C635F4AA2A964A020096F9FF /* MessageResponse.swift */,
C635F4A72A964A020096F9FF /* MerchantProfileProvider.swift */,
C635F4A82A964A020096F9FF /* MerchantProfileRequest.swift */,
C635F4A92A964A020096F9FF /* MerchantProfileData.swift */,
);
path = IO;
sourceTree = "<group>";
Expand All @@ -277,6 +279,7 @@
children = (
C635F4AD2A964A020096F9FF /* PayPalMessageViewParameters.swift */,
C635F4AE2A964A020096F9FF /* Log.swift */,
C635F4A92A964A020096F9FF /* MerchantProfileData.swift */,
C635F4AF2A964A020096F9FF /* Proxy.swift */,
C635F4B02A964A020096F9FF /* PayPalMessageAttributedStringBuilder.swift */,
C635F4B12A964A020096F9FF /* PayPalMessageViewParametersBuilder.swift */,
Expand Down Expand Up @@ -475,6 +478,7 @@
C635F4D82A964A020096F9FF /* MerchantProfileData.swift in Sources */,
C635F4DC2A964A020096F9FF /* Log.swift in Sources */,
C635F4C92A964A020096F9FF /* PayPalMessageView.swift in Sources */,
C61D84472B33348C00F372EF /* CloudEvent.swift in Sources */,
C635F4EB2A964A020096F9FF /* PayPalMessageModal.swift in Sources */,
C635F4D12A964A020096F9FF /* ImageAsset.swift in Sources */,
C635F4C82A964A020096F9FF /* PayPalMessageModalConfig.swift in Sources */,
Expand All @@ -486,11 +490,11 @@
C635F4CB2A964A020096F9FF /* PayPalMessageOfferType.swift in Sources */,
C635F4E72A964A020096F9FF /* CGFloat.swift in Sources */,
C635F4D52A964A020096F9FF /* MessageRequest.swift in Sources */,
C60F156A2B3C851100E16902 /* AnalyticsLogger.swift in Sources */,
C635F4E82A964A020096F9FF /* PayPalMessageModalDelegates.swift in Sources */,
C635F4C12A964A020096F9FF /* PayPalMessageModalViewModel.swift in Sources */,
C635F4D32A964A020096F9FF /* HTTPHeaders.swift in Sources */,
C635F4C22A964A020096F9FF /* ComponentLogger.swift in Sources */,
C635F4C32A964A020096F9FF /* ComponentLoggerEvent.swift in Sources */,
C635F4C32A964A020096F9FF /* AnalyticsEvent.swift in Sources */,
C635F4CA2A964A020096F9FF /* PayPalMessageViewModel.swift in Sources */,
C635F4E02A964A020096F9FF /* Fetch.swift in Sources */,
C635F4EA2A964A020096F9FF /* CloseButton.swift in Sources */,
Expand All @@ -500,7 +504,7 @@
C635F4D42A964A020096F9FF /* PayPalMessageLogoType.swift in Sources */,
C635F4DE2A964A020096F9FF /* PayPalMessageAttributedStringBuilder.swift in Sources */,
C635F4E42A964A020096F9FF /* UserDefaults.swift in Sources */,
C635F4C42A964A020096F9FF /* Logger.swift in Sources */,
C635F4C42A964A020096F9FF /* AnalyticsService.swift in Sources */,
C635F4C62A964A020096F9FF /* AnyCodable.swift in Sources */,
C635F4D02A964A020096F9FF /* PayPalMessagePlacement.swift in Sources */,
C635F4E92A964A020096F9FF /* PayPalMessageDelegates.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

enum ComponentLoggerEvent: Encodable {
enum AnalyticsEvent: Encodable {
case messageRender(renderDuration: Int, requestDuration: Int)
case messageClick(linkName: String, linkSrc: String)
case messageError(errorName: String, errorDescription: String)
Expand Down
100 changes: 100 additions & 0 deletions Sources/PayPalMessages/Analytics/AnalyticsLogger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import Foundation

class Weak<T: AnyObject> {

weak var value: T?

init(_ value: T?) {
self.value = value
}
}

class AnalyticsLogger: Encodable {

// Global Details
static var deviceID: String?
static var sessionID: String?
static var integrationVersion: String?
static var integrationName: String?

var component: Component

var instanceId: String

// Includes things like fdata, experience IDs, debug IDs, and the like
var dynamicData: [String: AnyCodable] = [:]

// Events tied to the component
var events: [AnalyticsEvent] = []

enum Component {
case message(Weak<PayPalMessageView>)
case modal(Weak<PayPalMessageModal>)
}

init(_ component: Component) {
self.instanceId = UUID().uuidString
self.component = component

AnalyticsService.shared.addLogger(self)
}

deinit {}

enum StaticKey: String, CodingKey {
// Integration Details
case offerType = "offer_type"
case amount = "amount"
case placement = "placement"
case buyerCountryCode = "buyer_country_code"
case channel = "channel"
// Message Only
case styleLogoType = "style_logo_type"
case styleColor = "style_color"
case styleTextAlign = "style_text_align"
// Other Details
case type = "type"
case instanceId = "instance_id"
// Component Events
case events = "component_events"
}

func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: StaticKey.self)

try container.encode(instanceId, forKey: .instanceId)
try container.encode(events, forKey: .events)
try dynamicData.encode(to: encoder)

switch component {
case .message(let weakMessage):
guard let message = weakMessage.value else { return }

try container.encode("message", forKey: .type)
try container.encodeIfPresent(message.offerType?.rawValue, forKey: .offerType)
try container.encodeIfPresent(message.amount, forKey: .amount)
try container.encodeIfPresent(message.placement?.rawValue, forKey: .placement)
try container.encodeIfPresent(message.buyerCountry, forKey: .buyerCountryCode)
try container.encodeIfPresent(message.logoType.rawValue, forKey: .styleLogoType)
try container.encodeIfPresent(message.color.rawValue, forKey: .styleColor)
try container.encodeIfPresent(message.alignment.rawValue, forKey: .styleTextAlign)

case .modal(let weakModal):
guard let modal = weakModal.value else { return }

try container.encode("modal", forKey: .type)
try container.encodeIfPresent(modal.offerType?.rawValue, forKey: .offerType)
try container.encodeIfPresent(modal.amount, forKey: .amount)
try container.encodeIfPresent(modal.placement?.rawValue, forKey: .placement)
try container.encodeIfPresent(modal.buyerCountry, forKey: .buyerCountryCode)
}
}

func addEvent(_ event: AnalyticsEvent) {
self.events.append(event)
}

func clearEvents() {
events.removeAll()
}
}
73 changes: 73 additions & 0 deletions Sources/PayPalMessages/Analytics/AnalyticsService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import Foundation

class AnalyticsService {

static let shared = AnalyticsService()

// Integration Details
let timerInterval: Double = 5
var timer: Timer?
var sender: LogSendable = LogSender()
var loggers: [AnalyticsLogger] = []


private init() {
timer = Timer.scheduledTimer(
withTimeInterval: timerInterval,
repeats: true
) { _ in
self.flushEvents()
}
}

deinit {
timer?.invalidate()
flushEvents()
}

func addLogger(_ logger: AnalyticsLogger) {
loggers.append(logger)
}

func clearEvents() {
for logger in loggers {
logger.clearEvents()
}
}

func flushEvents() {
let cloudEvents = CloudEvent.create(from: loggers)

guard !cloudEvents.isEmpty else { return }

for cloudEvent in cloudEvents {
if let cloudEventData = try? JSONEncoder().encode(cloudEvent) {
sender.send(cloudEventData, to: cloudEvent.environment)
}
}

clearEvents()
}
}

protocol LogSendable {
func send(_ data: Data, to environment: Environment)
}

class LogSender: LogSendable {

func send(_ data: Data, to environment: Environment) {
guard let url = environment.url(.log) else { return }
let headers: [HTTPHeader: String] = [
.acceptLanguage: "en_US",
.requestedBy: "native-checkout-sdk",
.accept: "application/json",
.contentType: "application/cloudevents+json"
]

log(.debug, "log_payload", with: data, for: environment)

fetch(url, method: .post, headers: headers, body: data, session: environment.urlSession) { _, _, _ in }
}
deinit {}
}
Loading