Implement Google Analytics in ios swift -


i following analytics ios (developers.google.com/analytics/devguides/collection/ios/v3/?ver=swift) guide , i've got errors in swift code project can't fix. working xcode 6.4, swift , ios deployment target 8.1.

step 1

first installed google sdk using cocoapods. console result after running pod install command:

updating local specs repositories  cocoapods 1.0.0.beta.2 available. update use: `gem install cocoapods --pre` [!] test version we'd love try.  more information see http://blog.cocoapods.org , changelog version http://git.io/bah8pq.  analyzing dependencies downloading dependencies using google (1.0.7) using googleanalytics (3.14.0) using googlenetworkingutilities (1.0.0) using googlesymbolutilities (1.0.3) using googleutilities (1.1.0) generating pods project integrating client project sending stats pod installation complete! there 1 dependency podfile , 5 total pods installed. 

step 2

then opened, said in guide, app's project .xcworkspace file.

my podfile looks this:

# uncomment line define global platform project # platform :ios, '8.0' # uncomment line if you're using swift # use_frameworks!  target 'xxxxxx'  source 'https://github.com/cocoapods/specs.git' platform :ios, '8.1' pod 'google/analytics', '~> 1.0.0'  end  target 'xxxxxxtests'  pod 'google/analytics', '~> 1.0.0'  end 

where xxxxxx project's name.

step 3

i got configuration file googleservice-info.plist , included in project adding targets (2 targets in project).

step 4

i created bridgingheader by choosing file > new > file > ios > source > header file. named bridgingheader.h , in root of project. content is:

#ifndef xxxxx_bridgingheader_h #define xxxxx_bridgingheader_h  #import "google/analytics.h" #import <google/analytics.h> #include "gai.h"  #import <coredata/coredata.h> #import <systemconfiguration/systemconfiguration.h>  #import "libraries/googleanalytics/gai.h" #import "libraries/googleanalytics/gaifields.h" #import "libraries/googleanalytics/gailogger.h" #import "libraries/googleanalytics/gaitracker.h" #import "libraries/googleanalytics/gaidictionarybuilder.h"  #endif 

where "xxxxx" project's name.

step 5

now problems: tried include/import google analytics appdelegate.swift can't. error:

appdelegate.swift import google analytics

i tried import "google/analytics.h" error appears: expected identifier in import declaration.

  • how can fix xcode doesn't give me errors?
  • is bridgingheader wrong? have point @ somehow recognize inner headers?
  • do have configure else google analytics missing right now?

thank much.

there 2 options implementation google analytics using cocoapods.

  1. pod 'google/analytics'
  2. pod 'googleanalytics'

there pros , cons between them.

pod 'google/analytics'

  • need google configuration file(googleservice-info.plist)
  • simple bridging header file. add #import <google/analytics.h> in bridging header file.
  • add import googlein every file want implement google analytics.

pod 'googleanalytics'

  • no google configuration file(googleservice-info.plist)
  • more complex bridging header file.

i prefer use pod 'googleanalytics' i'll explain how solve issue using pod 'google/analytics' because google official site recommends pod 'google/analytics'.

  1. bridging header

you need 1 line of code google analytics.

#import <google/analytics.h> 

don't forget set target-build setting objective-c-bridging-header. have provide correct path enable objective-c-bridging-header.

set target-build setting-objective-c-bridging-header $(srcroot)/$(product_name)/projectname_bridging_header.h

  1. appdelegate.swift
import google  func application(application: uiapplication, didfinishlaunchingwithoptions launchoptions: [nsobject: anyobject]?) -> bool { self.setupgoogleanalytics() ..      self.setupgoogleanalytics() ..  }  func setupgoogleanalytics() {      // configure tracker googleservice-info.plist.     let configureerror:nserror?     gglcontext.sharedinstance().configurewitherror(&configureerror)     assert(configureerror == nil, "error configuring google services: \(configureerror)")      let gai = gai.sharedinstance()     gai.trackuncaughtexceptions = true  // report uncaught exceptions     gai.logger.loglevel = gailoglevel.verbose  // remove before app release } 
  1. someviewcontroller.swift
override func viewwillappear(animated: bool) {     super.viewwillappear(true)      if let default_tracker = gai.sharedinstance().defaulttracker {         #if debug              print("default tracker")          #endif     }      //        let tracker = gai.sharedinstance().defaulttracker     let tracker = gai.sharedinstance().trackerwithtrackingid("tracking_id")     tracker.set(kgaiscreenname, value: screenname)     let builder = gaidictionarybuilder.createscreenview()     tracker.send(builder.build() [nsobject : anyobject])  } 

why use trackerwithtrackingid instead of defaulttracker property? got error if use defaulttracker :

fatal error: unexpectedly found nil while unwrapping optional value

defaulttracker property's initial value nil, set after trackerwithtrackingid method called. doesn't work sometimes. avoid issue, recommend use trackerwithtrackingid method directly.

i make sample project using pod 'googleanalytics'. can idea it. luck.

test env

googleanalytics 3.14

xcode 7.2.1


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -