Google Analytics for iOS with Swift
CocoaPodsを使用しないで、Google Analyticsを入れる方法
手動でGoogle Analyticsのスクリーン計測を行うまでをメモしておく。
環境
準備
Google Developers Download the SDKからからzipをダウンロードする。現時点でのバージョンは3.15
GoogleAnalytics/Library配下のファイルをプロジェクトに追加する
- GAI.h
- GAIDictionaryBuilder.h
- GAIEcommerceProduct.h
- GAIEcommerceProductAction.h
- GAIEcommercePromotion.h
- GAIFields.h
- GAILogger.h
- GAITrackedViewController.h
- GAITracker.h
Build PhasesのLink Binary With Librariesに以下を追加
Bridging headerを作成する(ex. BridgingHeader.h)
Build SettingsのObjective-C Bridging Headerに${PRODUCT_NAME}/BridgingHeader.hを記述
BridgingHeader.hに以下のインポート文を追加
// とりあえずLibrary配下のヘッダーをインポートする #import "GAI.h" #import "GAIDictionaryBuilder.h" #import "GAIEcommerceProduct.h" #import "GAIEcommerceProductAction.h" #import "GAIEcommercePromotion.h" #import "GAIFields.h" #import "GAILogger.h" #import "GAITrackedViewController.h" #import "GAITracker.h"
'GAI.h' file not found とBridgingHeaderでエラーがでるときは、Header Search Pathが設定されているか確認する。Build Settings -> Search Path - Header Search Paths 。設定されていない場合は、パスを追加 (例 $(SRCROOT)/** )。
※ #import <Google/Analytics.h>ができるのはPodからインストールしたときだけなのかな?
トラッキングIDを設定する
計測を行う前にトラッキングIDを設定する
GAI.sharedInstance().trackerWithTrackingId("UA-xxxxxx-x")
GoogleService-Info.plistを使用する場合は下でもOKなのかな?
var configureError:NSError? GGLContext.sharedInstance().configureWithError(&configureError) assert(configureError == nil, "Error configuring Google services: \(configureError)")
手動でスクリーン計測を行う(Automatic Screen Measurement)
var tracker = GAI.sharedInstance().defaultTracker tracker.set(kGAIScreenName, value: "Home Screen") // 計測するスクリーン名を記述する var params = GAIDictionaryBuilder.createScreenView().build() as [NSObject : AnyObject] tracker.send(params)