衡量广告收入

您可以使用 Google Analytics(分析)来衡量通过在创收平台(例如 AdMob、MoPub 和 ironSource)展示广告所带来的广告收入。衡量收入来源有助于您了解用户的生命周期价值并拓展应用业务。

准备工作

请确保您已按照 Google Analytics(分析)使用入门中的说明设置了您的项目。要使用广告收入衡量功能,您需要将 Firebase 项目与 Google Analytics(分析)帐号相关联,并在应用中安装 Android SDK v17.6.0 或者 iOS v6.34.0 或更高版本。

实现

要衡量广告收入,您必须记录 ad_impression 事件,包括广告平台、来源、货币和价值等详细信息。本指南介绍如何根据您使用的平台设置广告收入衡量功能。

AdMob

如果您使用的是 AdMob 平台,请将 AdMob 关联到 Firebase 和 Google Analytics(分析),以便自动衡量广告收入。每当用户看到展示的广告时,Google Analytics for Firebase SDK 都会自动记录 ad_impression 事件。

其他广告创收平台

如果您使用的是其他广告创收平台(例如 MoPub 或 ironSource),则可以通过手动记录 ad_impression 事件来跟踪展示级收入数据,如以下示例所示。

MoPub

Swift

注意:此 Firebase 产品不适用于 macOS 目标平台。

func mopubAd(_ ad: MPMoPubAd, didTrackImpressionWith impressionData: MPImpressionData?) {
  if let impressionData = impressionData {
    Analytics.logEvent(
      AnalyticsEventAdImpression,
      parameters: [
        AnalyticsParameterAdPlatform: "MoPub",
        AnalyticsParameterAdUnitName: impressionData.adUnitName,
        AnalyticsParameterAdFormat: impressionData.adUnitFormat,
        AnalyticsParameterValue: impressionData.publisherRevenue,
        AnalyticsParameterCurrency: impressionData.currency,
        AnalyticsParameterAdSource: impressionData.networkName,
        "precision": impressionData.precision,
      ])
  }
}

Objective-C

注意:此 Firebase 产品不适用于 macOS 目标平台。

- (void)mopubAd:(id)ad didTrackImpressionWithImpressionData:(MPImpressionData * _Nullable)impressionData{

    // Feed impression data into internal tools or send to third-party analytics
    if (impressionData != nil){
        [FIRAnalytics logEventWithName:kFIREventAdImpression
                parameters:@{
                             kFIRParameterAdPlatform:@"MoPub",
                             kFIRParameterAdUnitName:impressionData.adUnitName,
                             kFIRParameterAdFormat:impressionData.adunitFormat,
                             kFIRParameterValue:impressionData.publisherRevenue,
                             kFIRParameterCurrency:impressionData.currency,
                             kFIRParameterAdSource:impressionData.networkName,
                             @"precision":impressionData.precision
                             }];
                }
}

Java

@Override
public void onImpression(@NonNull final String adUnitId, @Nullable final ImpressionData impressionData) {

  if (impressionData != null) {
    // Feed impression data into internal tools or send to third-party analytics
    FirebaseAnalytics firebaseAnalytics = FirebaseAnalytics.getInstance(context);

    Bundle params = new Bundle();
    params.putString(FirebaseAnalytics.Param.AD_PLATFORM, "MoPub");
    params.putString(FirebaseAnalytics.Param.AD_SOURCE, impressionData.network_name);
    params.putString(FirebaseAnalytics.Param.AD_FORMAT, impressionData.adunit_format);
    params.putString(FirebaseAnalytics.Param.AD_UNIT_NAME, impressionData.adunit_name);
    params.putDouble(FirebaseAnalytics.Param.VALUE, impressionData.publisher_revenue);
    params.putString(FirebaseAnalytics.Param.CURRENCY, impressionData.currency);
    params.putString("precision", impressionData.precision);
    mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.AD_IMPRESSION, params);
  }

}

Kotlin

@Override
public void onImpression(@NonNull final String adUnitId, @Nullable final ImpressionData impressionData) {

  if (impressionData != null) {
    // Feed impression data into internal tools or send to third-party analytics
    FirebaseAnalytics firebaseAnalytics = FirebaseAnalytics.getInstance(context);

    firebaseAnalytics.logEvent(FirebaseAnalytics.Event.AD_IMPRESSION) {
      param(FirebaseAnalytics.Param.AD_PLATFORM, "MoPub")
      param(FirebaseAnalytics.Param.AD_SOURCE, impressionData.network_name)
      param(FirebaseAnalytics.Param.AD_FORMAT, impressionData.adunit_format)
      param(FirebaseAnalytics.Param.AD_UNIT_NAME, impressionData.adunit_name)
      param(FirebaseAnalytics.Param.VALUE, impressionData.publisher_revenue)
      param(FirebaseAnalytics.Param.CURRENCY, impressionData.currency)
      param("precision", impressionData.precision)
    }
  }
}

Unity

// REGISTER TO LISTEN TO THE EVENT FROM MOPUB SDK
private void OnEnable() {
    MoPubManager.OnImpressionTrackedEvent += OnImpressionTrackedEvent;
}

private void OnImpressionTrackedEvent(string adUnitId, MoPub.ImpressionData impressionData)
{
    // Feed impression data into internal tools or send to third-party analytics
    var myImpressionObject = JsonUtility.FromJson(impressionData.JsonRepresentation);
    var impressionParameters = new[] {
        new Firebase.Analytics.Parameter("ad_platform", "MoPub"),
        new Firebase.Analytics.Parameter("ad_source", myImpressionObject.network_name),
        new Firebase.Analytics.Parameter("ad_unit_name", myImpressionObject.adunit_name),
        new Firebase.Analytics.Parameter("ad_format", myImpressionObject.adunit_format),
        new Firebase.Analytics.Parameter("value", myImpressionObject.publisher_revenue),
        new Firebase.Analytics.Parameter("currency", myImpressionObject.currency),
        new Firebase.Analytics.Parameter("precision", myImpressionObject.precision)
    };
    Firebase.Analytics.FirebaseAnalytics.LogEvent("ad_impression", impressionParameters);
}

ironSource

Swift

注意:此 Firebase 产品不适用于 macOS 目标平台。

func impressionDataDidSucceed(_ impressionData: ISImpressionData!) {
  Analytics.logEvent(
    AnalyticsEventAdImpression,
    parameters: [
      AnalyticsParameterAdPlatform: "ironSource",
      AnalyticsParameterAdSource: impressionData.ad_network ?? "No ad_network",
      AnalyticsParameterAdFormat: impressionData.ad_unit ?? "No ad_unit",
      AnalyticsParameterAdUnitName: impressionData.instance_name ?? "No instance_name",
      AnalyticsParameterCurrency: "USD",
      AnalyticsParameterValue: impressionData.revenue ?? 0,
    ])
}

Objective-C

注意:此 Firebase 产品不适用于 macOS 目标平台。

- (void)impressionDataDidSucceed:(ISImpressionData *)impressionData {

        [FIRAnalytics logEventWithName:kFIREventAdImpression
                    parameters:@{
                        kFIRParameterAdPlatform:@"ironSource",
                        kFIRParameterAdSource:impressionData.ad_network,
                        kFIRParameterAdFormat:impressionData.ad_unit,
                        kFIRParameterAdUnitName:impressionData.instance_name,
                        kFIRParameterCurrency:@"USD",
                        kFIRParameterValue:impressionData.revenue
                    }];

}

Java

// Invoked when the ad was displayed successfully and the impression data was recorded
 @Override
 public void onImpressionSuccess(ImpressionData impressionData) {
     // The onImpressionSuccess will be reported when the rewarded video and interstitial ad is opened.
     // For banners, the impression is reported on load success. Log.d(TAG, "onImpressionSuccess" + impressionData);
     if (impressionData != null) {
         Bundle bundle = new Bundle();
         bundle.putString(FirebaseAnalytics.Param.AD_PLATFORM, "ironSource");
         bundle.putString(FirebaseAnalytics.Param.AD_SOURCE, impressionData.adNetwork());
         bundle.putString(FirebaseAnalytics.Param.AD_FORMAT, impressionData.getAdUnit());
         bundle.putString(FirebaseAnalytics.Param.AD_UNIT_NAME, impressionData.getAdUnit());
         bundle.putString(FirebaseAnalytics.Param.CURRENCY, "USD");
         bundle.putDouble(FirebaseAnalytics.Param.VALUE, impressionData.getRevenue());
         mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.AD_IMPRESSION, bundle);
     }
}

Kotlin

@Override
 public void onImpressionSuccess(ImpressionData impressionData) {
     // The onImpressionSuccess will be reported when the rewarded video and interstitial ad is opened.
     // For banners, the impression is reported on load success. Log.d(TAG, "onImpressionSuccess" + impressionData);
     if (impressionData != null) {
         firebaseAnalytics.logEvent(FirebaseAnalytics.Event.AD_IMPRESSION) {
         param(FirebaseAnalytics.Param.AD_PLATFORM, "ironSource")
         param(FirebaseAnalytics.Param.AD_SOURCE, impressionData.adNetwork())
         param(FirebaseAnalytics.Param.AD_FORMAT, impressionData.getAdUnit())
         param(FirebaseAnalytics.Param.AD_UNIT_NAME, impressionData.getAdUnit())
         param(FirebaseAnalytics.Param.CURRENCY, "USD")
         param(FirebaseAnalytics.Param.VALUE, impressionData.getRevenue())
     }
 }

Unity

private void ImpressionSuccessEvent(IronSourceImpressionData impressionData) {

    if (impressionData != null) {
        Firebase.Analytics.Parameter[] AdParameters = {
          new Firebase.Analytics.Parameter("ad_platform", "ironSource"),
          new Firebase.Analytics.Parameter("ad_source", impressionData.adNetwork),
          new Firebase.Analytics.Parameter("ad_unit_name", impressionData.getAdUnit),
          new Firebase.Analytics.Parameter("ad_format", impressionData.getInstanceName),
          new Firebase.Analytics.Parameter("currency","USD"),
          new Firebase.Analytics.Parameter("value", impressionData.getRevenue)
        };
        Firebase.Analytics.FirebaseAnalytics.LogEvent("ad_impression", AdParameters);
    }
}

后续步骤

在 Firebase 控制台中以及 Analytics(分析)控制台内关联的 Analytics(分析)媒体资源中访问 Analytics(分析)信息中心。广告收入会被纳入到 ARPU生命周期价值等指标中。