6

I have been working with google analytics fine for the past year and now I am switching to swift. I have a problem importing it using the pods [ I have done an extensive search and it seems a problem with [use_frameworks!] that is required by Alamofire.

I have added the SDK manually, that is libGoogleAnalyticsServices.a and imported some other files in a bridging file called header-Bridging-Header.h:

#import <Google/Analytics.h>
#import <libGoogleAnalyticsServices.a>
#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"

now in AppDelegate.swift I am trying to configure tracker from GoogleService-Info.plist.

    var configureError:NSError?
    GGLContext.sharedInstance().configureWithError(&configureError)
    if configureError != nil {
        println("Error configuring the Google context: \(configureError)")
    }

but it shows an error used of unresolved identifier GGLContext

Cœur
  • 37,241
  • 25
  • 195
  • 267
iShaalan
  • 799
  • 2
  • 10
  • 30
  • 1
    I had the same issue but I skipped that for now and just use the tracking id directly in code. `gai.trackerWithTrackingId("UA-xxxxxxxx-x")` – Altrim Jul 14 '15 at 12:52
  • @Altrim so you were able to track user interaction in the app? if so Are you using pods or static library libGoogleAnalyticsServices.a – iShaalan Jul 14 '15 at 13:34
  • I am using static library, not pods. You need to import `libGoogleAnalyticsServices` with the other libraries specified in the docs to your projects target "Linked Frameworks and Libraries". – Altrim Jul 14 '15 at 13:36
  • Also remove the first two lines where you import `` and ``. You don't need them there. And make sure to remove GoogleAnalytics from the podfile since you are adding them manually. – Altrim Jul 14 '15 at 13:38
  • @iShaalan With cocoapods V0.38.2 it is possible to use cocoapods with Google Analytics. http://stackoverflow.com/questions/30910852/the-pods-target-has-transitive-dependencies-that-include-static-binaries-whe/30933140#30933140 And because gia is an singleton, you can set the tracker id manually in code or from an API result as answered by Altrim – Gerrit Post Sep 18 '15 at 17:40
  • I've been able to integrate Google Analytics and Alamofire in the same Podfile with Swift 2.0 in Xcode 7. What does your Podfile look like? In the bridging header, I just needed to add Google Analytics: #import "GAI.h"#import "GAIFields.h" #import "GAIDictionaryBuilder.h" – r3c0d3 Oct 16 '15 at 16:43

2 Answers2

5

Google was a bit slow to properly support Cocopods but this has been resolved, now. The tricky bit now is to know which version of Google Analytics pod to use as there are at least three different ones, two of which are authored by Google themselves. For using GA using CocoaPods most likely you should be using the one they officially recommend using, which is listed here: https://developers.google.com/analytics/devguides/collection/ios/v3/?ver=swift

as of this writing the pod is pod 'Google/Analytics' - using this GA should work without additional effort and without directly embedding any libraries into your code. Additionally the only thing you need in you bridging header is this:

#import <Google/Analytics.h>

For a detailed explanation of why there are so may different pods and which one to use, see this video: https://www.youtube.com/watch?v=JQJd7qyWh5k

Cœur
  • 37,241
  • 25
  • 195
  • 267
Fuad Kamal
  • 1,162
  • 10
  • 14
1

into your cocoapods you need to set:

pod 'Google/Analytics'

If you want to use:

var configureError:NSError?
GGLContext.sharedInstance().configureWithError(&configureError)
if configureError != nil {
    println("Error configuring the Google context: \(configureError)")
}

If you use:

pod 'GoogleAnalytics' #(without '/')

GGLContext will not be available :)..

Cœur
  • 37,241
  • 25
  • 195
  • 267
douarbou
  • 2,283
  • 1
  • 21
  • 25