我尝试通过适用于Android的Google跟踪代码管理器设置通用分析代码。我有一个空白活动(布局中没有任何内容)如下:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_verification);
// Start GTM code
try
{
TagManager tagManager = TagManager.getInstance(this);
tagManager.setVerboseLoggingEnabled(true);
PendingResult<ContainerHolder> pending = tagManager.loadContainerPreferNonDefault("GTM-XXXXXX", R.raw.default_gtm_container);
pending.setResultCallback(new ResultCallback<ContainerHolder>()
{
@Override
public void onResult(ContainerHolder containerHolder)
{
ContainerHolderSingleton.setContainerHolder(containerHolder);
if (!containerHolder.getStatus().isSuccess())
{
Log.e("GoogleTagManager", "failure loading container");
return;
}
ContainerHolderSingleton.setContainerHolder(containerHolder);
containerHolder.setContainerAvailableListener(new ContainerHolder.ContainerAvailableListener()
{
@Override
public void onContainerAvailable(ContainerHolder containerHolder, String containerVersion)
{
containerHolder.getContainer();
}
});
}
}, 2, TimeUnit.SECONDS);
} catch (Exception e)
{
e.printStackTrace();
}
// End GTM code
}
@Override
public void onStart()
{
super.onStart();
DataLayer dataLayer = TagManager.getInstance(this).getDataLayer();
dataLayer.pushEvent("openScreen", DataLayer.mapOf("screenName", "Verification"));
}
我尝试做的只是在此活动开始时触发Universal Analytics代码。正如您在日志输出中看到的那样:
04-23 06:49:56.745: D/dalvikvm(4334): Not late-enabling CheckJNI (already on)
04-23 06:49:56.855: V/GoogleTagManager(4334): Atttempting to load container from resource ID 2131034112
04-23 06:49:56.855: W/GoogleTagManager(4334): Resource is a UTF-8 encoded string but doesn't contain a JSON container
04-23 06:49:56.865: V/GoogleTagManager(4334): Deleted 0 expired items
04-23 06:49:56.875: V/GoogleTagManager(4334): Start loading resource from disk ...
04-23 06:49:56.875: W/dalvikvm(4334): threadid=13: thread exiting with uncaught exception (group=0xb2d4fb20)
04-23 06:49:56.885: W/GoogleTagManager(4334): GooglePlayServicesNotAvailableException getting Advertising Id Info
04-23 06:49:56.885: E/AndroidRuntime(4334): FATAL EXCEPTION: pool-2-thread-1
04-23 06:49:56.885: E/AndroidRuntime(4334): Process: com.outing.planner, PID: 4334
04-23 06:49:56.885: E/AndroidRuntime(4334): java.lang.NullPointerException
04-23 06:49:56.885: E/AndroidRuntime(4334): at com.google.android.gms.tagmanager.o$b.a(Unknown Source)
04-23 06:49:56.885: E/AndroidRuntime(4334): at com.google.android.gms.tagmanager.o$b.i(Unknown Source)
04-23 06:49:56.885: E/AndroidRuntime(4334): at com.google.android.gms.tagmanager.cq.jC(Unknown Source)
04-23 06:49:56.885: E/AndroidRuntime(4334): at com.google.android.gms.tagmanager.cq$1.run(Unknown Source)
04-23 06:49:56.885: E/AndroidRuntime(4334): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-23 06:49:56.885: E/AndroidRuntime(4334): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-23 06:49:56.885: E/AndroidRuntime(4334): at java.lang.Thread.run(Thread.java:841)
04-23 06:49:56.915: D/dalvikvm(4334): GC_FOR_ALLOC freed 239K, 9% free 2999K/3292K, paused 17ms, total 17ms
04-23 06:49:57.225: D/(4334): HostConnection::get() New Host Connection established 0xb8c8e590, tid 4334
04-23 06:49:57.245: W/EGL_emulation(4334): eglSurfaceAttrib not implemented
04-23 06:49:57.245: D/OpenGLRenderer(4334): Enabling debug mode 0
04-23 06:50:01.945: V/GoogleTagManager(4334): connecting to Analytics service
04-23 06:50:01.945: V/GoogleTagManager(4334): connect: bindService returned false for Intent { act=com.google.android.gms.analytics.service.START cmp=com.google.android.gms/.analytics.service.AnalyticsService (has extras) }
04-23 06:50:01.945: W/GoogleTagManager(4334): Service unavailable (code=1), will retry.
04-23 06:50:01.945: V/GoogleTagManager(4334): Loaded clientId
04-23 06:50:01.945: I/GoogleTagManager(4334): No campaign data found.
04-23 06:50:01.945: V/GoogleTagManager(4334): Initialized GA Thread
04-23 06:50:01.945: W/GoogleTagManager(4334): GooglePlayServicesNotAvailableException getting Ad Id Info
04-23 06:50:01.945: V/GoogleTagManager(4334): putHit called
04-23 06:50:06.965: V/GoogleTagManager(4334): connecting to Analytics service
04-23 06:50:06.965: V/GoogleTagManager(4334): connect: bindService returned false for Intent { act=com.google.android.gms.analytics.service.START cmp=com.google.android.gms/.analytics.service.AnalyticsService (has extras) }
04-23 06:50:06.965: W/GoogleTagManager(4334): Service unavailable (code=1), using local store.
04-23 06:50:06.965: V/GoogleTagManager(4334): falling back to local store
04-23 06:50:06.965: V/GoogleTagManager(4334): Sending hit to store PATH: https: PARAMS: v=1, ul=en-us, t=appview, ht=1398250196938, sr=480x800, an=OutingPlannerAndroid, tid=UA-XXXXXXXX-Y, aid=com.outing.planner, cid=c5f3e490-a578-49d6-b0e7-eaf3bce8dfe6, av=1.0, _u=.4nL, cd=Verification,
04-23 06:50:06.965: V/GoogleTagManager(4334): PowerSaveMode initiated.
04-23 06:50:06.975: V/GoogleTagManager(4334): PowerSaveMode terminated.
04-23 06:50:06.975: V/GoogleTagManager(4334): Dispatch running...
04-23 06:50:07.405: D/dalvikvm(4334): GC_FOR_ALLOC freed 336K, 11% free 3177K/3568K, paused 6ms, total 6ms
04-23 06:50:08.215: V/GoogleTagManager(4334): sent 1 of 1 hits
04-23 06:50:08.225: V/GoogleTagManager(4334): PowerSaveMode initiated.
虽然引发了NullPointerException
,但实际上已发送Google Analytics点击,我看到了实时报告中的点击。 NullPointerException
导致我的应用程序崩溃,即使在应用程序崩溃后也会发送Google Analytics匹配。
为什么NullPointerException
被抛出?它似乎被Google跟踪管理器课程抛出,但我不知道我做错了什么。我跟着CuteAnimalV4示例项目一直到最后一个字母。
请注意,我已隐藏Google跟踪代码管理器容器ID和Google Analytics帐户ID。这些是现实生活中的有效价值。
答案 0 :(得分:3)
根据这个帖子https://productforums.google.com/forum/#!topic/tag-manager/87Kb_TZv79w
将在下一个Google Play服务版本中修复。现在,您可以添加未捕获的异常处理程序作为解决方法。