Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 8 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ let package = Package(
.target(name: "SkipFirebaseAnalytics", dependencies: [
"SkipFirebaseCore",
.product(name: "FirebaseAnalytics", package: "firebase-ios-sdk", condition: .when(platforms: [.macOS, .iOS, .tvOS, .watchOS, .macCatalyst])),
.product(name: "SkipUI", package: "skip-ui"),
], resources: [.process("Resources")], plugins: skipstone),
.testTarget(name: "SkipFirebaseAnalyticsTests", dependencies: [
"SkipFirebaseAnalytics",
Expand Down Expand Up @@ -147,9 +148,15 @@ let package = Package(
)

if ProcessInfo.processInfo.environment["SKIP_BRIDGE"] ?? "0" != "0" {
package.dependencies += [.package(url: "https://source.skip.tools/skip-fuse.git", "0.0.0"..<"2.0.0")]
package.dependencies += [
.package(url: "https://source.skip.tools/skip-fuse.git", "0.0.0"..<"2.0.0"),
.package(url: "https://source.skip.tools/skip-fuse-ui.git", from: "1.10.0"),
]
package.targets.forEach({ target in
target.dependencies += [.product(name: "SkipFuse", package: "skip-fuse")]
if target.name == "SkipFirebaseAnalytics" {
target.dependencies += [.product(name: "SkipFuseUI", package: "skip-fuse-ui")]
}
})
// all library types must be dynamic to support bridging
package.products = package.products.map({ product in
Expand Down
49 changes: 49 additions & 0 deletions Sources/SkipFirebaseAnalytics/SkipFirebaseAnalyticsSwiftUI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// SPDX-License-Identifier: LGPL-3.0-only WITH LGPL-3.0-linking-exception
#if !SKIP_BRIDGE
#if SKIP
import SwiftUI

struct LoggedAnalyticsModifier: ViewModifier {
/// The name of the view to log in the `AnalyticsParameterScreenName` parameter.
let screenName: String

/// The name of the view to log in the `AnalyticsParameterScreenClass` parameter.
let screenClass: String

/// Extra parameters to log with the screen view event.
let extraParameters: [String: Any]

func body(content: Content) -> some View {
// Take the content and add an onAppear action to know when the view has appeared on screen.
content.onAppear {
// Log the event appearing, adding the appropriate keys and values needed for screen
// view events.
var parameters = extraParameters
parameters[AnalyticsParameterScreenName] = screenName
parameters[AnalyticsParameterScreenClass] = screenClass
Analytics.logEvent(AnalyticsEventScreenView, parameters: parameters)
}
}
}

public extension View {
/// Logs `screen_view` events in Google Analytics for Firebase when this view appears on screen.
/// - Parameters:
/// - name: Current screen name logged with the `screen_view` event.
/// - class: Current screen class or struct logged with the `screen_view` event.
/// - extraParameters: Any additional parameters to be logged. These extra parameters must
/// follow the same rules as described in the `Analytics.logEvent(_:parameters:)` docs.
/// - Returns: A view with a custom `ViewModifier` used to log `screen_view` events when this
/// view appears on screen.
func analyticsScreen(name: String,
class: String = "View",
extraParameters: [String: Any] = [:]) -> some View {
// `self` is the view, we're just adding an `LoggedAnalyticsModifier` modifier on it.
modifier(LoggedAnalyticsModifier(screenName: name,
screenClass: `class`,
extraParameters: extraParameters))
}
}

#endif
#endif
Loading