跟踪Google Analytics中的Admob事件

时间:2014-07-06 09:10:46

标签: android google-analytics admob

我希望使用Google Analytics跟踪AdMob横幅广告的点击次数,但是会出现问题并且我不明白为什么。

目前,我的AdMob横幅实现如下:
布局:

<com.google.android.gms.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ads:adUnitId="YOUR_AD_UNIT_ID"
    ads:adSize="BANNER"/>

Java:AdView adView = (AdView)this.findViewById(R.id.adView);

但是,显示如何添加AdListener(project available here)的Google演示项目未在布局中指定任何内容,并使用以下代码添加横幅:

LinearLayout layout = (LinearLayout) findViewById(R.id.leLinearLayoutDeMonChoix);
layout.addView(adView);

但如果使用开头描述的实现,则AdListener不会再检测到任何事件。为什么呢?

您可以在以下演示项目中找到此有缺陷的实施:https://drive.google.com/file/d/0B8rE1pbtzNJ1UXg5QllubEFidGc/edit?usp=sharing

我提前感谢您的时间和帮助。

1 个答案:

答案 0 :(得分:1)

在提供的实现中,您正在执行以下操作:

// Create an ad.
adView = new AdView(this);
// Set the AdListener.
adView.setAdListener(new AdListener() {
    /** stuff **/
}
AdView adView = (AdView)this.findViewById(R.id.adView);
// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
    .addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
    .build();
// Start loading the ad in the background.
adView.loadAd(adRequest);

有两个AdView实例,这是您的问题。

  • 首先通过setContentView()
  • 扩展main.xml布局而创建的
  • 当你写adView = new AdView(this);
  • 时的第二个

您可以在第二个上设置侦听器,但只显示第一个侦听器。 它不能工作。 :)
选择一种方法(从布局创建)或另一种方法(从代码创建),但不要混淆它们。

如果您想从布局中制作广告,请执行以下操作:

// Retreive the adView.
AdView adView = (AdView)this.findViewById(R.id.adView);
// Set the AdListener.
adView.setAdListener(new AdListener() {
    /** stuff **/
}
// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
    .addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
    .build();
// Start loading the ad in the background.
adView.loadAd(adRequest);