Implement Google Analytics in ios swift

前端 未结 5 857
半阙折子戏
半阙折子戏 2021-02-02 00:53

I am following the Analytics for iOS (developers.google.com/analytics/devguides/collection/ios/v3/?ver=swift) guide and I\'ve got errors in my Swift code Project that I can\'t f

5条回答
  •  感动是毒
    2021-02-02 01:27

    In Podfile

    pod 'Google/Analytics'
    

    In YourFantasticProjectName-Bridging-Header.h

    #import "Google/Analytics.h"
    

    You don't need this

    GGLContext.sharedInstance().configureWithError(&configureError)
    

    You need to have a proper tracker

    let gai = GAI.sharedInstance()
    let tracker = gai.tracker(withTrackingId: "UA-12345678-1")
    

    In order for live view to work in GA dashboard, you must track screen using GAIDictionaryBuilder and correct key kGAIScreenName

    tracker.set(kGAIScreenName, value: "this is my screen")
    let event = GAIDictionaryBuilder.createScreenView()
    tracker?.send(event!.build() as! [NSObject: Any])
    

    In the same vein, to track events, you need to use GAIDictionaryBuilder as it will create dictionary with correct GA keys, and GA likes correct keys

    let event = GAIDictionaryBuilder.createEvent(withCategory: "category", action: "action", label: "level", value: NSNumber(value: 120))
    tracker?.send(event!.build() as! [NSObject: Any])
    

    It seems to work in the simulator too

提交回复
热议问题