20

I'm trying to use GA with a Swift project.

I installed the SDK correctly but I can't send Screen Measurements manually because some objects are not found.

Here's the code given by Google:

// May return nil if a tracker has not already been initialized with a
// property ID.
id tracker = [[GAI sharedInstance] defaultTracker];

// This screen name value will remain set on the tracker and sent with
// hits until it is set to a new value or to nil.
[tracker set:kGAIScreenName
   value:@"Home Screen"];

// New SDK versions
[tracker send:[[GAIDictionaryBuilder createScreenView] build]];

Here's my code:

let tracker = GAI.sharedInstance()
tracker.setValue(kGai, forKey: "/index")
tracker.send(GAIDictionaryBuilder.createScreenView().build)

And here's the errors I get:

Use of unresolved identifier 'kGAIScreenName'
Use of unresolved identifier 'GAIDictionaryBuilder'

I imported GAI.h in my BridingHeader and added frameworks to the build file, no errors on this side.

Thanks!

Skoua
  • 3,373
  • 3
  • 38
  • 51

2 Answers2

27

OK so I just added GAI.h to my bridging header but didn't add others header files. And thanks DPLusV I also didn't translated correctly Obj-C to Swift.

Here is my final code which works:

let tracker = GAI.sharedInstance().defaultTracker
tracker.set(kGAIScreenName, value: "/index")
tracker.send(GAIDictionaryBuilder.createScreenView().build())

[EDIT] SWIFT 3

let tracker = GAI.sharedInstance().defaultTracker
tracker?.set(kGAIScreenName, value: "/index")
let build = (GAIDictionaryBuilder.createScreenView().build() as NSDictionary) as! [AnyHashable: Any]
tracker?.send(build)
Skoua
  • 3,373
  • 3
  • 38
  • 51
  • 1
    thanks, this worked for me. wish google added some documentation for swift - since I don't know ObjC all I can do is guess how to transform it to swift syntax... – Camillo Jan 05 '15 at 20:13
  • 12
    Make sure you `#import "GAIFields.h"` in order to use `kGAIScreenName` – Jonathan Ellis Jan 18 '15 at 17:52
  • 5
    @I use Xcode 6.3beta with Swift 1.2 and get the following error: Cannot invoke 'send' with an argument list of type '(NSMutableDictionary!)' – Michael Apr 12 '15 at 16:44
  • Here is a similar question: http://stackoverflow.com/questions/29591930/google-analytics-not-working-with-swift-1-2-and-xcode-6-3 – Michael Apr 12 '15 at 16:50
  • @JonathanEllis also "#import "GAIDictionaryBuilder.h" – paul Sep 10 '17 at 01:52
  • create Bridging header file https://stackoverflow.com/questions/24146677/swift-bridging-header-import-issue/26729748 – Tajinder singh Aug 26 '21 at 06:52
5

I followed the instructions in

https://developers.google.com/analytics/devguides/collection/ios/v3/?ver=swift

up till pod installation

I already had a GoogleService-Info.plist file there I enabled the google analytics

Then I did some hit and trial methods to make it work

And finally I found some thing which worked for me

let googleAnalytics : GAITracker = GAI.sharedInstance().trackerWithTrackingId("UA-XXXXXXXX-X")
GAI.sharedInstance().trackUncaughtExceptions = true
googleAnalytics.set(kGAIScreenName, value: screenName)
let builder = GAIDictionaryBuilder.createScreenView()
googleAnalytics.send(builder.build() as [NSObject : AnyObject])

I put this piece of code in View Controller(s) where ever it was needed.

Hope this help someone. Thanks

bably
  • 1,065
  • 5
  • 17
  • 27
  • 1
    get sample app from https://github.com/phanisai4u/sampleAppsSwift/tree/master/GoogleAnalyticsSampleSwift – Phani Sai Dec 15 '15 at 11:15