Help us understand the problem. What is going on with this article?

【SwiftUI】FirebaseAnalytics用にModifierを作ってみた

この記事について

Firebase×SwiftUIで作成されたアプリの場合
どのようにして画面が表示された際に
EventやUserPropertyを送ろうか考えた際に試してみた内容を記事にしてみました。

最後の方におまけとしてこれもできるのかな〜と
と思ったことを試してみたのも添えてありますのでご興味があればぜひ!

ViewのExtensionにAnalytics用のメソッドを追加

Viewのextensionで追加してみる

FirebaseAnalyticsTracker.swift
final class FirebaseAnalyticsTracker {
    enum Event: String {
        case login
    }

    func sendEventLog(_ event: Event, parameters: [String: Any] = [:]) {
        Analytics.logEvent(event.rawValue, parameters: parameters)
    }

    enum UserProperty: String {
        case level
    }

    func setUserProperty(key: UserProperty, value: String) {
        Analytics.setUserProperty(value, forName: key.rawValue)
    }
}
extension View {
    func sendEventLog(_ eventName: FirebaseAnalyticsTracker.Event, parameters: [String: Any] = [:]) -> Self {
        FirebaseAnalyticsTracker().sendEventLog(eventName, parameters: parameters)
        return self
    }

    func setUserProperty(key: FirebaseAnalyticsTracker.UserProperty, value: String) -> Self {
        FirebaseAnalyticsTracker().setUserProperty(key: key, value: value)
        return self
    }
}
struct FirebaseAnalyticsTestView: View {
    var body: some View {
        VStack {
            Text("Send Analytics Data to Firebase")
        }
        .sendEventLog(.login)
        .setUserProperty(key: .level, value: "5")
    }
}

DebugViewで確認

無事loginEventとlevelが5であるというUserPropertyが送れてました:v:
スクリーンショット 2019-10-16 1.14.36.png

課題

@State@ObservedObjectで変更が通知され
Viewが再描画される際にFirebaseAnalytics用のModifierがある場合余分にEvent,UserPropertyが送信されているようでした。

ここに関しては引き続き検証をしていこうと思っておりますので進捗あり次第更新します:bow_tone2:

おまけ

Viewにコメント付与,logを出すModifierもできる

extension View {
    func comment(_ text: String) -> Self {
        return self
    }

    func log(_ text: String) -> Self {
        print(text)
        return self
    }
}

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Send Analytics Data to Firebase")
                .comment("こんな感じでいける")
                .log("text")
        }
    }
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした