在Google Analytics中跟踪Admob事件

3

我希望能够使用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)(可在此处获取项目)没有在布局中指定任何内容,并使用以下代码添加横幅广告:

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

但是如果使用开头描述的实现方式,AdListener将不再检测到任何事件。为什么呢?
您可以在以下演示项目中找到这个有缺陷的实现:https://drive.google.com/file/d/0B8rE1pbtzNJ1UXg5QllubEFidGc/edit?usp=sharing 非常感谢您的时间和帮助。
1个回答

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);

确实,现在看来很明显。谢谢! - LegZ

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接