在iOS SWIFT中集成谷歌分析

时间:2022-05-22 14:28:43

It's my first time integrating google analytics in my app.I'm just following this official document here

这是我第一次将谷歌分析集成到我的应用程序中。

I already have a tracker ID. I don't want to create configuration file. How can I use that tracker ID and how can I integrate Google analytics?

我已经有一个跟踪器ID,我不想创建配置文件。如何使用跟踪器ID以及如何集成谷歌分析?

1 个解决方案

#1


1  

Create extension of UIView Conroller.

extension UIViewController {
    func setScreeName(name: String) {
        self.title = name
        self.sendScreenView()
    }

    func sendScreenView() {
        let tracker = GAI.sharedInstance().defaultTracker
        tracker.set(kGAIScreenName, value: self.title)
        let builder = GAIDictionaryBuilder.createScreenView()
        tracker.send(builder.build() as [NSObject : AnyObject])
    }

    func trackEvent(category: String, action: String, label: String, value: NSNumber?) {
        let tracker = GAI.sharedInstance().defaultTracker
        let trackDictionary = GAIDictionaryBuilder.createEventWithCategory(category, action: action, label: label, value: value)
        tracker.send(trackDictionary.build() as [NSObject : AnyObject])
    }
}

For Each view Controller viewdidload() you add following code
self.title = self.navigationItem.title!
    self.sendScreenView() 

#1


1  

Create extension of UIView Conroller.

extension UIViewController {
    func setScreeName(name: String) {
        self.title = name
        self.sendScreenView()
    }

    func sendScreenView() {
        let tracker = GAI.sharedInstance().defaultTracker
        tracker.set(kGAIScreenName, value: self.title)
        let builder = GAIDictionaryBuilder.createScreenView()
        tracker.send(builder.build() as [NSObject : AnyObject])
    }

    func trackEvent(category: String, action: String, label: String, value: NSNumber?) {
        let tracker = GAI.sharedInstance().defaultTracker
        let trackDictionary = GAIDictionaryBuilder.createEventWithCategory(category, action: action, label: label, value: value)
        tracker.send(trackDictionary.build() as [NSObject : AnyObject])
    }
}

For Each view Controller viewdidload() you add following code
self.title = self.navigationItem.title!
    self.sendScreenView()