LoginSignup
53
53

More than 5 years have passed since last update.

iOS向けGoogle Analytics v3の設定についてメモ

Last updated at Posted at 2013-12-10

間違ってたり足りない部分もあるかも知れないが、忘れると何一つ思い出せないため、覚えている範囲でメモ。
あと、Googleの書いてるドキュメントのとおりだとうまくいかない箇所があったので気をつけてください。
Google Analytics SDK for iOS v3
https://developers.google.com/analytics/devguides/collection/ios/v3/
trackedViewNameではなくscreenNameを使います。(v2→v3移行ガイドでは正しく書いてあるのですが。https://developers.google.com/analytics/devguides/collection/ios/v3/migration)
2013/12/10

①Hoge-Prefix.pchに追記 (ここに書いておけばすべてのクラスに書かずに済む)

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    ...
    // Google アナリティクス
    #import "GAI.h" //※ここを追記 
#endif

②AppDelegateのapplication:didFinishLaunchingWithOptions:に書く

// オプション: 例外を自動的にGoogle Analyticsに送る.
[GAI sharedInstance].trackUncaughtExceptions = YES;
// オプション: トラッキング間隔 20秒
[GAI sharedInstance].dispatchInterval = 20;
// オプション: よくわかんないからコメントアウトした
//[GAI sharedInstance].debug = YES;
// トラッカーのインスタンス作成
id<GAITracker> tracker = [[GAI sharedInstance] trackerWithTrackingId:@"トラッキングIDを記入"];

③ビューコントローラーに書く(自動タイプ)

// GAITrackedViewController.hをインポート
#import "GAITrackedViewController.h"

//UIViewControllerのサブクラスから、GAITrackedViewControllerに変更する
@interface HogeViewController : GAITrackedViewController

//viewDidAppear:に以下の一文を追加
self.screenName = @"HogeViewController";

④テーブルビューコントローラーに書く(手動タイプ)
Use of undeclared identifier 'kGAIScreenName' というエラーが出るときは#import "GAIFields.h"も追記する。

// インポート
#import "GAIFields.h"
#import "GAIDictionaryBuilder.h"

//viewDidLoad:に以下を追加
[[[GAI sharedInstance] defaultTracker] set:kGAIScreenName value:@"HogeTableViewController"];
[[[GAI sharedInstance] defaultTracker] send:[[GAIDictionaryBuilder createAppView] build]];

ライブラリの追加もお忘れなく。

53
53
1

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
53
53