# How to log revenue to Firebase, Appsflyer, Adjust

### 1. IronSource (LevelPlay)

Subscribe to events (used for all ad formats: banner, interstitial, rewarded)

```csharp
IronSourceEvents.onImpressionDataReadyEvent += OnImpressionDataReadyEvent;
```

Log revenue when the event returns:

```csharp
private static void OnImpressionDataReadyEvent(IronSourceImpressionData impressionData)
{
    if (impressionData?.revenue == null) return;
    
    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.instanceName),
        new Firebase.Analytics.Parameter("ad_format", impressionData.adUnit),
        new Firebase.Analytics.Parameter("currency","USD"),
        new Firebase.Analytics.Parameter("value", impressionData.revenue.Value)
    };
    FirebaseAnalytics.LogEvent("ad_impression", adParameters);
        
//If using AppsFlyer
    var dic = new Dictionary<string, string>
    {
        { "ad_unit_name", impressionData.instanceName },
        { "ad_format", impressionData.adUnit }
    };
    
    //For SDK 6.15.0 and above. Uses the ad revenue SDK API.
    var logRevenue = new AFAdRevenueData(impressionData.adNetwork, MediationNetwork.IronSource, "USD", impressionData.revenue.Value);
        AppsFlyer.logAdRevenue(logRevenue, dic);
    
    //For SDK 6.14.2 and below. Uses the ad revenue SDK connector.
    AppsFlyerAdRevenue.logAdRevenue(impressionData.adNetwork, AppsFlyerAdRevenueMediationNetworkType.AppsFlyerAdRevenueMediationNetworkTypeIronSource, 
        impressionData.revenue.Value, "USD", dic);
        
//If using Adjust (skip this part if using AppsFlyer)
    AdjustAdRevenue adjustAdRevenue = new AdjustAdRevenue(AdjustConfig.AdjustAdRevenueSourceIronSource);
    adjustAdRevenue.setRevenue(impressionData.revenue.Value, "USD");
    adjustAdRevenue.setAdRevenueNetwork(impressionData.adNetwork);
    adjustAdRevenue.setAdRevenueUnit(impressionData.adUnit);
    adjustAdRevenue.setAdRevenuePlacement(impressionData.placement);
    Adjust.trackAdRevenue(adjustAdRevenue);
}
```

### 2. Admob

All AdMob ad formats return a [micro value](https://developers.google.com/admob/unity/impression-level-ad-revenue?hl=vi#advalue), so before logging, you must multiply by 0.000001 to convert to the value calculated in USD. Below is an example of logging revenue for AdMob's Collapsible Banner format. Other formats are logged similarly:

```csharp
// Raised when the ad is estimated to have earned money. 
_bannerView.OnAdPaid += (AdValue adValue) =>
{
    if (adValue == null) return;    
    double value = adValue.Value * 0.000001f;
    
    Firebase.Analytics.Parameter[] adParameters = {
        new Firebase.Analytics.Parameter("ad_source", "admob"),
        new Firebase.Analytics.Parameter("ad_format", "collapsible_banner"),
        new Firebase.Analytics.Parameter("currency","USD"),
        new Firebase.Analytics.Parameter("value", value)
    };
    FirebaseAnalytics.LogEvent("ad_impression", adParameters);
    
//If using AppsFlyer
    var dic = new Dictionary<string, string>
    {
        { "ad_format", "collapsible_banner" }
    };
    
    //For SDK 6.15.0 and above. Uses the ad revenue SDK API.
    var logRevenue = new AFAdRevenueData("Admob", MediationNetwork.GoogleAdMob, "USD", value);
        AppsFlyer.logAdRevenue(logRevenue, dic);
    
    //For SDK 6.14.2 and below. Uses the ad revenue SDK connector.
    AppsFlyerAdRevenue.logAdRevenue("Admob", AppsFlyerAdRevenueMediationNetworkType.AppsFlyerAdRevenueMediationNetworkTypeGoogleAdMob, 
        value, "USD", dic);
        
//If using Adjust (skip this part if using AppsFlyer)
    AdapterResponseInfo loadedAdapterResponseInfo = _bannerView.GetResponseInfo().GetLoadedAdapterResponseInfo();
    AdjustAdRevenue adRevenue = new AdjustAdRevenue(AdjustConfig.AdjustAdRevenueSourceAdMob);
    adRevenue.setRevenue(value, "USD");
    adRevenue.setAdRevenueNetwork(loadedAdapterResponseInfo.AdSourceName);
    Adjust.trackAdRevenue(adRevenue);
};
```

### 3. Max&#x20;

Listen for the OnAdRevenuePaidEvent for the formats being used (ignore the formats not in use):

```csharp
MaxSdkCallbacks.Banner.OnAdRevenuePaidEvent += OnAdRevenuePaidEvent;
MaxSdkCallbacks.Interstitial.OnAdRevenuePaidEvent += OnAdRevenuePaidEvent;
MaxSdkCallbacks.Rewarded.OnAdRevenuePaidEvent += OnAdRevenuePaidEvent;
```

Perform logging of revenue when the event returns. Note: [the value of the returned revenue may be -1 in case of an error](https://developers.applovin.com/en/unity/overview/advanced-settings/#impression-level-user-revenue-api). Therefore, it is necessary to have a code snippet to check if revenue > 0 before logging.

```csharp
private void OnAdRevenuePaidEvent(string message, MaxSdkBase.AdInfo adInfo)
{
    if (adInfo == null) return;
    if (adInfo.Revenue < 0) return;

    Firebase.Analytics.Parameter[] adParameters = {
        new Firebase.Analytics.Parameter("ad_platform", "Applovin"),
        new Firebase.Analytics.Parameter("ad_source", adInfo.NetworkName),
        new Firebase.Analytics.Parameter("ad_unit_name", adInfo.AdUnitIdentifier),
        new Firebase.Analytics.Parameter("ad_format", adInfo.AdFormat),
        new Firebase.Analytics.Parameter("currency","USD"),
        new Firebase.Analytics.Parameter("value", adInfo.Revenue)
    };
    FirebaseAnalytics.LogEvent("ad_impression", adParameters);
        
//If using AppsFlyer
    var dic = new Dictionary<string, string>
    {
        { "ad_unit_name", adInfo.AdUnitIdentifier },
        { "ad_format", adInfo.AdFormat }
    };
    
    //For SDK 6.15.0 and above. Uses the ad revenue SDK API.
    var logRevenue = new AFAdRevenueData(adInfo.NetworkName, MediationNetwork.ApplovinMax, "USD", adInfo.Revenue);
        AppsFlyer.logAdRevenue(logRevenue, dic);
    
    //For SDK 6.14.2 and below. Uses the ad revenue SDK connector.
    AppsFlyerAdRevenue.logAdRevenue(adInfo.NetworkName, AppsFlyerAdRevenueMediationNetworkType.AppsFlyerAdRevenueMediationNetworkTypeApplovinMax, 
        adInfo.Revenue, "USD", dic);
        
 //If using Adjust (skip this part if using AppsFlyer)
    var adRevenue = new AdjustAdRevenue(AdjustConfig.AdjustAdRevenueSourceAppLovinMAX);
    adRevenue.setRevenue(adInfo.Revenue, "USD");
    adRevenue.setAdRevenueNetwork(adInfo.NetworkName);
    adRevenue.setAdRevenueUnit(adInfo.AdUnitIdentifier);
    adRevenue.setAdRevenuePlacement(adInfo.Placement);
    Adjust.trackAdRevenue(adRevenue);            
}
```
