Skip to content

Commit 1edcd32

Browse files
authored
Support custom tracking when page views are tracked (#1124)
1 parent 3ebfc89 commit 1edcd32

File tree

9 files changed

+64
-19
lines changed

9 files changed

+64
-19
lines changed

Demo/Sources/Application/AppDelegate.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66

77
import AVFAudio
88
import Combine
9+
import os
910
import PillarboxAnalytics
1011
import ShowTime
1112
import SRGDataProvider
1213
import UIKit
1314

1415
final class AppDelegate: NSObject, UIApplicationDelegate {
16+
private static let logger = Logger(category: "AppDelegate")
1517
private var cancellables = Set<AnyCancellable>()
1618

1719
// swiftlint:disable:next discouraged_optional_collection
@@ -48,7 +50,7 @@ final class AppDelegate: NSObject, UIApplicationDelegate {
4850
sourceKey: .developmentSourceKey,
4951
appSiteName: "pillarbox-demo-apple"
5052
)
51-
try? Analytics.shared.start(with: configuration, dataSource: self)
53+
try? Analytics.shared.start(with: configuration, dataSource: self, delegate: self)
5254
}
5355
}
5456

@@ -65,3 +67,13 @@ extension AppDelegate: AnalyticsDataSource {
6567
])
6668
}
6769
}
70+
71+
extension AppDelegate: AnalyticsDelegate {
72+
func didTrackPageView(commandersAct commandersActPageView: CommandersActPageView) {
73+
Self.logger.debug("[didTrackPageView] commandersAct: \(commandersActPageView.name)")
74+
}
75+
76+
func didSendEvent(commandersAct commandersActEvent: CommandersActEvent) {
77+
Self.logger.debug("[didSendEvent] commandersAct: \(commandersActEvent.name)")
78+
}
79+
}

Demo/Sources/Showcase/ShowcaseView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ struct ShowcaseView: View {
399399
)
400400
cell(
401401
title: "Video URN",
402-
destination: .optInPlayer(media: URNMedia.onDemandVerticalVideo)
402+
destination: .optInPlayer(media: URNMedia.onDemandHorizontalVideo)
403403
)
404404
}
405405
.sourceCode(of: OptInView.self)

Sources/Analytics/Analytics.docc/PillarboxAnalytics.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Before submitting your app to production, validate it, especially after signific
5656
### Configuration
5757

5858
- ``AnalyticsDataSource``
59+
- ``AnalyticsDelegate``
5960
- ``CommandersActGlobals``
6061
- ``ComScoreGlobals``
6162
- ``ComScoreConsent``

Sources/Analytics/Analytics.swift

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,28 @@ public class Analytics {
5757
private let commandersActService = CommandersActService()
5858

5959
private weak var dataSource: AnalyticsDataSource?
60+
private weak var delegate: AnalyticsDelegate?
6061

6162
private init() {}
6263

6364
/// Starts analytics with the specified configuration.
6465
///
6566
/// - Parameters:
6667
/// - configuration: The configuration to use.
67-
/// - dataSource: The data source to use.
68+
/// - dataSource: The object that acts as the data source of the analytics.
69+
/// - delegate: The object that acts as the delegate of the analytics.
6870
///
6971
/// This method must be called from your `UIApplicationDelegate.application(_:didFinishLaunchingWithOptions:)`
7072
/// delegate method implementation, otherwise the behavior is undefined.
7173
///
7274
/// The method throws if called more than once.
73-
public func start(with configuration: Configuration, dataSource: AnalyticsDataSource? = nil) throws {
75+
public func start(with configuration: Configuration, dataSource: AnalyticsDataSource? = nil, delegate: AnalyticsDelegate? = nil) throws {
7476
guard self.configuration == nil else {
7577
throw AnalyticsError.alreadyStarted
7678
}
7779
self.configuration = configuration
7880
self.dataSource = dataSource
81+
self.delegate = delegate
7982

8083
UIViewController.setupViewControllerTracking()
8184

@@ -90,22 +93,22 @@ public class Analytics {
9093
commandersActService.trackPageView(
9194
commandersActPageView.merging(globals: dataSource?.commandersActGlobals)
9295
)
96+
delegate?.didTrackPageView(commandersAct: commandersActPageView)
9397
}
9498

9599
/// Sends an event.
96100
///
97101
/// - Parameter commandersActEvent: The Commanders Act event data.
98102
public func sendEvent(commandersAct commandersActEvent: CommandersActEvent) {
103+
sendCommandersActEvent(commandersActEvent)
104+
delegate?.didSendEvent(commandersAct: commandersActEvent)
105+
}
106+
}
107+
108+
extension Analytics {
109+
func sendCommandersActEvent(_ commandersActEvent: CommandersActEvent) {
99110
commandersActService.sendEvent(
100111
commandersActEvent.merging(globals: dataSource?.commandersActGlobals)
101112
)
102113
}
103114
}
104-
105-
public extension String {
106-
/// The source key for apps in production.
107-
static let productionSourceKey = "1b30366c-9e8d-4720-8b12-4165f468f9ae"
108-
109-
/// The source key for apps in development.
110-
static let developmentSourceKey = "39ae8f94-595c-4ca4-81f7-fb7748bd3f04"
111-
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//
2+
// Copyright (c) SRG SSR. All rights reserved.
3+
//
4+
// License information is available from the LICENSE file.
5+
//
6+
7+
/// A delegate for analytics events.
8+
public protocol AnalyticsDelegate: AnyObject {
9+
func didTrackPageView(commandersAct commandersActPageView: CommandersActPageView)
10+
func didSendEvent(commandersAct commandersActEvent: CommandersActEvent)
11+
}

Sources/Analytics/CommandersAct/CommandersActEvent.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66

77
/// A Commanders Act event.
88
public struct CommandersActEvent {
9-
let name: String
10-
let labels: [String: String]
9+
/// The event name.
10+
public let name: String
11+
12+
/// Additional information associated with the event.
13+
public let labels: [String: String]
1114

1215
/// Creates a Commanders Act event.
1316
///

Sources/Analytics/CommandersAct/CommandersActPageView.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@
66

77
/// A Commanders Act page view.
88
public struct CommandersActPageView {
9-
let name: String
10-
let type: String
11-
let levels: [String]
12-
let labels: [String: String]
9+
/// The page name.
10+
public let name: String
11+
12+
/// The page type (e.g., _Article_).
13+
public let type: String
14+
15+
/// Additional information associated with the page view.
16+
public let levels: [String]
17+
18+
/// The page levels.
19+
public let labels: [String: String]
1320

1421
/// Creates a Commanders Act page view.
1522
///

Sources/Analytics/CommandersAct/CommandersActTracker.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private extension CommandersActTracker {
8181
case (.none, _) where event != .play, (.eof, _) where event != .play:
8282
break
8383
default:
84-
Analytics.shared.sendEvent(commandersAct: .init(
84+
Analytics.shared.sendCommandersActEvent(.init(
8585
name: event.rawValue,
8686
labels: labels(from: properties)
8787
))

Sources/Analytics/Extensions/String.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66

77
import Foundation
88

9+
public extension String {
10+
/// The source key for apps in production.
11+
static let productionSourceKey = "1b30366c-9e8d-4720-8b12-4165f468f9ae"
12+
13+
/// The source key for apps in development.
14+
static let developmentSourceKey = "39ae8f94-595c-4ca4-81f7-fb7748bd3f04"
15+
}
16+
917
extension String {
1018
var isBlank: Bool {
1119
trimmingCharacters(in: .whitespacesAndNewlines).isEmpty

0 commit comments

Comments
 (0)