Google Analytics for iOS - 手动屏幕跟踪功能无效

时间:2021-04-15 15:21:22

I've got a problem with screen tracking on iOS. I'm doing it like it's presented in sample app from Google:

我在iOS上遇到屏幕跟踪问题。我这样做就像它在谷歌的示例应用程序中呈现的那样:

    let tracker = GAI.sharedInstance().defaultTracker
    tracker.set("My screen", value: "Value")
    tracker.send(GAIDictionaryBuilder.createScreenView().build() as [NSObject : AnyObject])

And it doesn't work. It's correctly tracking events though, and when I'm subclassing View Controller as GAITrackedViewController it's tracking screens. But, I don't want to subclass my view controllers just to track screens. Does anyone got an idea?

它不起作用。它正确地跟踪事件,当我将View Controller子类化为GAITrackedViewController时,它是跟踪屏幕。但是,我不想将视图控制器子类化为跟踪屏幕。有没有人有个主意?

1 个解决方案

#1


1  

You do not have to inherit your ViewControllers, just do this:

您不必继承ViewControllers,只需执行以下操作:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    let googleTracker = GAI.sharedInstance().trackerWithTrackingId("UA-YOUR_TRACKER_ID")
    googleTracker.set(kGAIScreenName, value: "MyPageName")
    let builder = GAIDictionaryBuilder.createScreenView()
    googleTracker.send(builder.build() as [NSObject : AnyObject])
}

Of course you do not need to create the "googleTracker" object every time, do it on some central place and get it from there.

当然,您不需要每次都创建“googleTracker”对象,在某个中心位置创建它并从那里获取它。

#1


1  

You do not have to inherit your ViewControllers, just do this:

您不必继承ViewControllers,只需执行以下操作:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    let googleTracker = GAI.sharedInstance().trackerWithTrackingId("UA-YOUR_TRACKER_ID")
    googleTracker.set(kGAIScreenName, value: "MyPageName")
    let builder = GAIDictionaryBuilder.createScreenView()
    googleTracker.send(builder.build() as [NSObject : AnyObject])
}

Of course you do not need to create the "googleTracker" object every time, do it on some central place and get it from there.

当然,您不需要每次都创建“googleTracker”对象,在某个中心位置创建它并从那里获取它。