LoginSignup
12
11

More than 5 years have passed since last update.

Swift環境でGoogleAnalyticsを設定する

Posted at

CocoaPodsのインストールと設定

基本的には以下、公式のページを参考にする
https://developers.google.com/analytics/devguides/collection/ios/v3/?ver=swift&hl=ja

ターミナルで自分のプロジェクト配下に移動
$cd hogehoge

CocoaPodsのインストール
$ pod init
$ pod install

完了だと問題ないが、「Updating local specs repositories」って出て終わらない場合、以下のようにする
$ pod repo remove master
Removing spec repo master
$ pod setup
Setting up CocoaPods master repo
Setup completed
5分近くかかるので気長に待つこと

無事に完了したら、Podfileを書き換える。
※「pod 'GoogleAnalytics'」がGoogle Analyticsを使いますよっていう記述になる
vi Podfile

# Uncomment this line to define a global platform for your project
platform :ios, '8.0'
# Uncomment this line if you're using Swift
use_frameworks!

target 'PJ名' do
pod 'GoogleAnalytics'
end

target 'PJ名Tests' do

end

target 'PJ名UITests' do

end

設定ファイルの取得と設定

公式ページに移動して「設定ファイルを取得」をクリックし、アプリを作成する
https://developers.google.com/analytics/devguides/collection/ios/v3/?ver=swift&hl=ja
最終的に「GoogleService-Info.plist」っていうファイルがDLできたらOK

「GoogleService-Info.plist」をXCode上で「Info-plist」と同じ階層(ルート)にコピーする
※この時、TargetsでPJ名にチェックをつけることを忘れずに

GoogleAnalyticsを設定する

以下ページを参考にしました。
http://qiita.com/ysk_1031/items/245ff929ea76f0eda11d

初期設定

まず、アプリ内でGoogleAnalticsを利用できるように、AppDelegate.swiftで初期化設定。

AppDelegate.swift
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // G/A初期設定
        let gai = GAI.sharedInstance()
        gai.trackUncaughtExceptions = true

        if let path = NSBundle.mainBundle().pathForResource("GoogleService-Info", ofType: "plist") {
            if let propertyList = NSDictionary(contentsOfFile: path) as? [String: AnyObject] {
                let trackingId = propertyList["TRACKING_ID"] as! String
                gai.trackerWithTrackingId(trackingId)
            }
        }

        ()

    }

end

トラッキングできるようにする設定

GATrackingManager.swift
import Foundation

class GATrackingManager {

    class func sendScreenTracking(screenName: String) {
        let tracker = GAI.sharedInstance().defaultTracker
        tracker.set(kGAIScreenName, value: screenName)
        tracker.send(GAIDictionaryBuilder.createScreenView().build() as [NSObject: AnyObject])
        tracker.set(kGAIScreenName, value: nil)
    }

    class func sendEventTracking(Category: String, Action: String, Label: String) {
        let tracker = GAI.sharedInstance().defaultTracker
        tracker.send(GAIDictionaryBuilder.createEventWithCategory(Category, action: Action, label: Label, value: nil).build() as [NSObject: AnyObject])
    }

}

※参考ページはスクリーントラッキングのみだったので、イベントトラッキングもできるように追記しました

トラッキングイベントを設定する

スクリーントラッキング

ViewController.swift
import UIKit

class ViewController: UIViewController {

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        GATrackingManager.sendScreenTracking("スクリーン名")
    }

end

イベントトラッキング

ViewController.swift
import UIKit

class ViewController: UIViewController {

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        GATrackingManager.sendEventTracking("カテゴリ名", Action: "アクション名", Label: "ラベル名")
    }

end
12
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
11