Google Analytics not working with Swift 1.2 and Xc

2020-02-05 13:06发布

I tried to use Google Analytics with my Swift 1.2 app using Xcode 6.3beta.

My bridging header works fine and contains:

#import "GAI.h"
#import "GAIDictionaryBuilder.h"
#import "GAIEcommerceFields.h"
#import "GAIEcommerceProduct.h"
#import "GAIEcommerceProductAction.h"
#import "GAIEcommercePromotion.h"
#import "GAIFields.h"
#import "GAILogger.h"
#import "GAITrackedViewController.h"
#import "GAITracker.h"

I tried the following Swift code to track a page:

  var tracker2:GAITracker = GAI.sharedInstance().defaultTracker as GAITracker
  tracker2.set(kGAIScreenName, value:"Home Screen")
  tracker2.send(GAIDictionaryBuilder.createScreenView().build())

But the last line above raises the following error:

Cannot invoke 'send' with an argument list of type '(NSMutableDictionary!)'

I found similar questions like: Google Analytics not initialising in Swift, Using Google Analytics with Swift on iOS

What do I have to change to make the code above working with Swift 1.2?

1条回答
欢心
2楼-- · 2020-02-05 13:41

I had the same problem. Here is how I got it to work

let build = GAIDictionaryBuilder.createAppView().set(screenName, forKey: kGAIScreenName).build() as NSDictionary
tracker.send(build as [NSObject : AnyObject])
查看更多
登录 后发表回答