iOS 中的 Google Analytics(不工作)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18829584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google Analytics in iOS (Not Working)
提问by Anil Kothari
I am trying to implement google analytics.. could you guys please help me
我正在尝试实施谷歌分析..你们能帮我吗
-(void) setGoogleAnalytics{
// Initialize tracker.
self.tracker = [[GAI sharedInstance] trackerWithName:@"ipad app"
trackingId:kTrackingID];
NSDictionary *appDefaults = @{kAllowTracking: @(YES)};
[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
// User must be able to opt out of tracking
[GAI sharedInstance].optOut =
![[NSUserDefaults standardUserDefaults] boolForKey:kAllowTracking];
// Optional: automatically send uncaught exceptions to Google Analytics.
[GAI sharedInstance].trackUncaughtExceptions = YES;
// Optional: set Google Analytics dispatch interval to e.g. 20 seconds.
[GAI sharedInstance].dispatchInterval = 5;
// Optional: set Logger to VERBOSE for debug information.
[[[GAI sharedInstance] logger] setLogLevel:kGAILogLevelVerbose];
[[GAI sharedInstance] setTrackUncaughtExceptions:YES];
}
and calling it in
并调用它
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
[self setGoogleAnalytics];
//
//
//
}
Inside My ViewController implementation
在我的 ViewController 实现中
[self dispatchEvent:@"Purchase Done"];
[self trackViewName:NSStringFromClass([self class])];
-(void) trackViewName:(NSString *) strClassName{
[[GAI sharedInstance] defaultTracker];
self.screenName=[NSString stringWithFormat:@"%@",strClassName];
[self.tracker send:[[NSDictionary alloc] initWithObjectsAndKeys:strClassName,@"ViewName", nil]];
[[GAI sharedInstance] dispatch];
}
- (void)dispatchEvent:(NSString *)strButtonText{
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
[tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"ui_action" // Event category (required)
action:@"button_press" // Event action (required)
label:strButtonText // Event label
value:nil] build]]; // Event value = [[GAI sharedInstance] defaultTracker];
[[GAI sharedInstance] dispatch];
}
Which version of google analytics, I should download currently I have downloaded google GoogleAnalyticsServicesiOS_3.01.zip (Recommended) as I dont want to work with the beta version GoogleAnalyticsiOS_2.0beta4.zip
我应该下载哪个版本的 google 分析,目前我已经下载了 google GoogleAnalyticsServicesiOS_3.01.zip(推荐),因为我不想使用测试版 GoogleAnalyticsiOS_2.0beta4.zip
回答by incmiko
UPDATE -- Google Analytics SDK for iOS v3
更新 -适用于 iOS v3 的 Google Analytics SDK
So I'm using v3, and there is not any problem:
所以我用的是v3,没有任何问题:
I'm implemented it in the AppDelegate. In .h file:
我在 AppDelegate 中实现了它。在 .h 文件中:
#import "GAI.h"
@property (nonatomic,assign) id<GAITracker> tracker; // I'm not using ARC (assign)
.m:
.m:
#import "GAIDictionaryBuilder.h"
#import "GAIFields.h"
// GOOGLE ANALYTICS
[GAI sharedInstance].trackUncaughtExceptions = YES;
[GAI sharedInstance].dispatchInterval = 0;
tracker = [[GAI sharedInstance] trackerWithTrackingId:@"yourGAID"];
And write a method like this:
并写一个这样的方法:
- (void) sendGoogleAnalyticsView:(NSString*)viewName{
[tracker set:kGAIScreenName value:viewName];
[tracker send:[[GAIDictionaryBuilder createAppView] build]];
[[GAI sharedInstance] dispatch]; // this will force track your views.
}
Old answer:
旧答案:
See this answer below this link, if you do it the same as I told in this answer it must work
请参阅此链接下方的此答案,如果您按照我在此答案中所说的方法进行操作,则它必须起作用
Another stack-overflow answered question about google-analytics
另一个堆栈溢出回答了有关 google-analytics 的问题
and use these methods:
并使用这些方法:
[GAI sharedInstance].optOut = YES;
[GAI sharedInstance].dispatchInterval = 0;
[GAI sharedInstance].trackUncaughtExceptions = YES;
tracker = [[GAI sharedInstance] trackerWithTrackingId:@"YOUR TRACKERID"];
[tracker sendView:@"Your View name"];
[tracker sendEventWithCategory:@"YOUR CATEGORY" withAction:@"YOUR ACTION" withLabel:nil withValue:nil];
Download GoogleAnalyticsiOS_2.0beta4.zip from this linkthis will contain those classes what you need, and it will work perfectly. Be careful, google analytics got a lead time, to show you information, about real time. And not real time datas will show only a day after
从这个链接下载 GoogleAnalyticsiOS_2.0beta4.zip这将包含你需要的那些类,它会完美地工作。小心,谷歌分析有一个提前期,向你显示实时信息。而不是实时数据只会在一天后显示
EDIT for 3.0:
编辑 3.0:
I found some probably useful things for you:
我发现了一些可能对你有用的东西:
We have just come across this issue and this is slightly out of date so here is an updated answer. The issue we were having after following the instructions on the Google Analytics website, they instruct you to add the following files
GAI.h
,GAIDictionaryBuilder.h
,GAILogger.h
,GAITrackedViewController.h
,GAITracker.h
andlibGoogleAnalytics_debug.a
library. What they completely forget to include on the website instructions is the one where you have to includelibGoogleAnalyticsServices.a
library. This is included in the zipped download but there is no instructions to indicate to include this in the debug version.Note : In the readme.txt
libGoogleAnalyticsServices.a
is just referred to aslibGoogleAnalytics.a
Google have failed to update their documentation to include the new name or the correct instructions that indicate this is required in debug.Files and Libraries that most be included
我们刚刚遇到了这个问题,这有点过时了,所以这里有一个更新的答案。我们继谷歌Analytics(分析)网站上的说明后遇到的问题,他们指导您添加以下文件
GAI.h
,GAIDictionaryBuilder.h
,GAILogger.h
,GAITrackedViewController.h
,GAITracker.h
和libGoogleAnalytics_debug.a
图书馆。他们完全忘记在网站说明中包含的内容是您必须包含libGoogleAnalyticsServices.a
库的内容。这包含在压缩下载中,但没有说明指示将其包含在调试版本中。注意:在 readme.txt
libGoogleAnalyticsServices.a
中只是被称为libGoogleAnalytics.a
Google 未能更新他们的文档以包含新名称或指示在调试中需要的正确说明。大多数包含的文件和库
GAI.h
GAIDictionaryBuilder.h
GAIFields.h
GAILogger.h
GAITrackedViewController.h
GAITracker.h
libGoogleAnalytics.a // Also know as libGoogleAnalyticsServices.a
libGoogleAnalytics_debug.a
plus information:
加信息:
I'm pretty sure google has not yet provided a arm64 version of their
libGoogleAnalyticsServices.a
, which is really annoying ...it has been weeks since the public the release of Xcode 5GM.For now, I guess only build for armv7, armv7s or remove google analytics until they get their head out of their pants.
我很确定谷歌还没有提供他们
libGoogleAnalyticsServices.a
.目前,我想只为 armv7、armv7s 构建或删除谷歌分析,直到他们从他们的裤子中脱身。
Here is a iOS Getting Started Guide.for implement it.
这是iOS 入门指南。为实现它。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Optional: automatically send uncaught exceptions to Google Analytics.
[GAI sharedInstance].trackUncaughtExceptions = YES;
// Optional: set Google Analytics dispatch interval to e.g. 20 seconds.
[GAI sharedInstance].dispatchInterval = 0;
// Optional: set Logger to VERBOSE for debug information.
[[[GAI sharedInstance] logger] setLogLevel:kGAILogLevelVerbose];
// Initialize tracker.
id<GAITracker> tracker = [[GAI sharedInstance] trackerWithTrackingId:@"UA-XXXX-Y"];
}
To manually send a screen view, set the screen field values on the tracker, then send the hit:
要手动发送屏幕视图,请在跟踪器上设置屏幕字段值,然后发送命中:
// May return nil if a tracker has not already been initialized with a
// property ID.
id tracker = [[GAI sharedInstance] defaultTracker];
// This screen name value will remain set on the tracker and sent with
// hits until it is set to a new value or to nil.
[tracker set:kGAIScreenName
value:@"Home Screen"];
[tracker send:[[GAIDictionaryBuilder createAppView] build]];
Or Automatic Screen Measurement:
或自动屏幕测量:
Automatically measure views as screens using the
GAITrackedViewController
class. Have each of your view controllers extendGAITrackedViewController
and add a property called screenName. This property will be used to set the screen name field.
使用
GAITrackedViewController
类自动测量作为屏幕的视图 。让每个视图控制器扩展GAITrackedViewController
并添加一个名为 screenName 的属性。此属性将用于设置屏幕名称字段。
//
// MyViewController.h
// An example of using automatic screen tracking in a ViewController.
//
#import "GAITrackedViewController.h"
// Extend the provided GAITrackedViewController for automatic screen
// measurement.
@interface AboutViewController : GAITrackedViewController
@end
//
// MyViewController.m
//
#import "MyViewController.h"
#import "AppDelegate.h"
@implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Set screen name.
self.screenName = @"Home Screen";
}
// Rest of the ViewController implementation.
@end
Event tracking:
事件追踪:
To send an event to Google Analytics, use GAIDictionaryBuilder.createEventWithCategory:action:label:value: and send the hit, as in this example:
要将事件发送到 Google Analytics,请使用 GAIDictionaryBuilder.createEventWithCategory:action:label:value: 并发送命中,如下例所示:
// May return nil if a tracker has not already been initialized with a property
// ID.
id<GAITracker> = [[GAI sharedInstance] defaultTracker];
[tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"ui_action" // Event category (required)
action:@"button_press" // Event action (required)
label:@"play" // Event label
value:nil] build]]; // Event value
回答by Brenwell
I had the same problem with 3.01. My problem was actually in the Google Analytics admin section.
我在 3.01 上遇到了同样的问题。我的问题实际上出在 Google Analytics 管理部分。
I had a profile setup for mobile (which was actually setup as if it were a website) which worked with version 1.x. However it appears that Google has now implented "mobile" profiles as well. I believe that the 3.x mobile SDKs cannot track to "Web" profiles.
我有一个适用于 1.x 版的移动配置文件设置(实际上设置为好像它是一个网站)。然而,谷歌现在似乎也实施了“移动”配置文件。我相信 3.x 移动 SDK 无法跟踪到“Web”配置文件。
Create a new profile by following the instructions here. And then use the new tracking Id and it should start tracking.
按照此处的说明创建新配置文件。然后使用新的跟踪 ID,它应该开始跟踪。
Note: Do not set the dispatchInterval to 0, as others have suggested, this also prevented tracking, set it to 1. This fixed everything for me.
注意:不要将 dispatchInterval 设置为 0,正如其他人所建议的那样,这也会阻止跟踪,将其设置为 1。这为我解决了所有问题。
回答by Rubaiyat Jahan Mumu
In the AppDElegate.mfile :
在AppDElegate.m文件中:
#import "AppDelegate.h"
#import "GAI.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GAI sharedInstance].trackUncaughtExceptions = YES;
[GAI sharedInstance].dispatchInterval = 1;
[[[GAI sharedInstance] logger]setLogLevel:kGAILogLevelVerbose];
id<GAITracker> tracker = [[GAI sharedInstance] trackerWithTrackingId:@"TrackingId"];
[GAI sharedInstance].defaultTracker = tracker;
return YES;
}
In ViewController.h
在ViewController.h 中
#import <UIKit/UIKit.h>
#import "GAITrackedViewController.h"
@interface FirstViewController : GAITrackedViewController
@end
In ViewController.m
在ViewController.m 中
- (void)viewDidLoad {
[super viewDidLoad];
self.screenName = @"RED Screen";
}
Try it. This worked for me just fine. I tried it more than three apps. And All are working for realtime. If your account for your app is newthen you might have to waitfor 24h or more to see the result. Sometimes it take time to display realtime data without any reason.
尝试一下。这对我来说很好。我尝试了三个以上的应用程序。并且所有人都在实时工作。如果您的应用程序帐户是新帐户,那么您可能需要等待24 小时或更长时间才能看到结果。有时无缘无故地显示实时数据需要时间。
Sometimes it also does not work because of old Google Analytics sdk. To get the latest sdk you can use cocoa pod. here is the procedure :
有时它也会因为旧的 Google Analytics sdk 而不起作用。要获得最新的 sdk,您可以使用可可豆荚。这是程序:
platform :ios, '10.0'
target “GoogleAnalyticsTestApp” do
pod 'GoogleAnalytics'
end
Write these lines in your pod file in your project directory and save it. then install pod. To know how to install cocoa pod see the link :
将这些行写入项目目录中的 pod 文件中并保存。然后安装pod。要了解如何安装可可豆荚,请参阅链接:
Error importing Google Analytics iOS SDK using Cocoa Pods
使用 Cocoa Pods 导入 Google Analytics iOS SDK 时出错
Do not write Google/Analytics. Write GoogleAnalytics. Hope it will solve the problem.
不要写谷歌/分析。编写谷歌分析。希望它能解决问题。
回答by Ankur SIngh
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@protocol GAITracker;
@interface GAITrackedViewController : UIViewController {
@private
id<GAITracker> tracker_;
NSString *trackedViewName_;
}
@property(nonatomic, assign)id<GAITracker> tracker;
@property(nonatomic, copy)NSString *trackedViewName;
@end
Paste this code on "GAITrackedViewController.h" file
Then you can use self.trackedName = @"Some Name"; easly.