How to Import Googleanalytics Header into a Library Framework

Import GoogleAnalytics headers into a Swift Framework?

Ok, I have solved it.
First I replaced the 'Google/Analytics' pod to the 'GoogleAnalytics' (without slash) because the first is deprecated.
Second in the project where I'm using my framework, in it bridging file I set follow:

#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"'

instead of #import <Google/Analytics.h>

Third I removed GoogleService-Info.plist file from the project and add GUI tracker ID setup to code, as described in Google documents.
Done.

Can't find headers for GoogleAnalytics-iOS-SDK with Cocoapods 0.37, Swift, frameworks

This is a bug in Cocoapods 0.37.0. It has been logged in their tracker as issue #3499.

Workaround

  • Add $(SRCROOT)/Pods/GoogleAnalytics-iOS-SDK to the User Header Search Paths (set to Recursive) in the app target's Build Settings.
  • Reference the Google Analytics header directly in the Bridging Header with #import "GAI.h"

Include of non-modular header inside framework module - Xcode 9.2

First, select the GoogleAnalytics SDK folder and drag and drop inside the framework app. Then add all the .h file as a public in Build Phases-> Headers-> public.

And also you need to add the below supporting framework in General ->

Embedded Binaries:
SystemConfiguration.framework
CoreData.framework
ImageIO.framework
libz.tbd
libsqlite3.tbd
libGoogleAnalytics.a

Cleaned and run build your framework project and it will run successfully.

Import Objective-c framework into Swift framework (Google Analytics + Cocoapod)

The solution is not as straightforward as for an app. We have to create a modulemap.

Have a look at this example repo.

Inside Swift code we can only import so called modules. The trick is to define a module which in turn contains all the ObjC headers that we need the Swift code to access.

The module map section of this article may also help you.

As convenient as the bridging header is, it has one key limitation—you can’t use it inside a framework project. The alternative is to use a module.

To do this, create a directory in your project directory named after the library you want to use. I did this in the shell, outside Xcode’s auspices, naming it CommonCrypto. Inside the directory, create a module.map file that encapsulates library settings. For CommonCrypto, module.map looks like this:

module CommonCrypto [system] {
header "/usr/include/CommonCrypto/CommonCrypto.h"
export *
}

Now add the new module to Import Paths under Swift Compiler – Search Paths in your project settings. Use ${SRCROOT} in the module path (e.g. ${SRCROOT}/CommonCrypto) to insure that the project works no matter where it’s checked out.

This makes it possible to just import CommonCrypto in your Swift files. Note that consumers of any frameworks you build using this technique are also going to have to add the module to their Swift search paths.

I followed the procedure in the article above and made it work for my need this way:

module Muse {
header "Muse.framework/Headers/Muse.h"
export *
}

I removed the [system] lexon for safety (as it removes warning) and put the framework inside the same folder as the module.map file.

Also, don't forget to include libc++.tbd and other required dependencies in your framework target (in the Linked Frameworks and Libraries section of the General tab) if you need them.

Headers necessary to add google analytics application in iOS

The way you add Google Analytic to your project in swift is as following:

  1. Download the Framework from Google (Get Configuration file) or use CocoPods to install it in your pod.
  2. Once you add it to your Project, make sure you create a Bridging-Header.h file to include the Google Analytics Modules. Following are necessary.


#import "GAI.h"
#import "GAITracker.h"
#import "GAIFields.h"
#import "GAIDictionaryBuilder.h"



  1. Open you appDelegate.swift file and add this to your didFinishLaunchingWithOptions: method. Make sure it match your Analytic Publisher ID/Tracking ID


let Tracker = GAI.sharedInstance()
Tracker.trackerWithTrackingId("UA-XXXXXXX-X")

Now you can capture events and view by integrating them in your handlers or viewDidAppear: method.

Event Example:

let eventTracker: NSObject = GAIDictionaryBuilder.createEventWithCategory(
"App",
action: "View",
label: "Application Launched",
value: nil).build()

Tracker.defaultTracker.send(eventTracker as! [NSObject: AnyObject])

Please ask any questions you may have. :)



Related Topics



Leave a reply



Submit