AppMetrica - cleveradssolutions/CAS-Track-revenue GitHub Wiki

Integrate the SDK into your app


The integration of AppMetrica SDK into your application is presented in the official source.

Track CAS revenue

Initialization

Android

More information about initialization of CAS SDK here and AppMetrica SDK here.

public class GlobalApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        
        //Initialize CAS
        CAS.buildManager().initialize(); 

        String key = "{Your_API_key}";
        // Creating an extended library configuration.
        YandexMetricaConfig config = YandexMetricaConfig.newConfigBuilder({Your_API_key}).build();
        // Initializing the AppMetrica SDK.
        YandexMetrica.activate(getApplicationContext(), config);
        // Automatic tracking of user activity.
        YandexMetrica.enableActivityAutoTracking(this);
    }
}

Replace {Your_API_key} with your API key. The API key is a unique application identifier that is issued in the AppMetrica web interface during app registration.

iOS

More information about initialization of CAS SDK here and AppMetrica SDK here.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool
{
    // Initialize CAS
    let manager = CAS.create(managerID: "demo",
                             enableTypes: [.banner, .interstitial, .rewarded],
                             demoAdMode: true) { complete, error in
               print("[CAS Sample] Mediation manager initialization: \(complete) with error: \(String(describing: error))")
           }

    // Initializing the AppMetrica SDK.
    let configuration = YMMYandexMetricaConfiguration.init(apiKey: "{Your_API_key}")
    YMMYandexMetrica.activate(with: configuration!) 
}

Replace {Your_API_key} with your API key. The API key is a unique application identifier that is issued in the AppMetrica web interface during app registration.

Unity3d

More information about initialization of CAS SDK here and AppMetrica SDK here.

Initialize CAS SDK

class CleverAdsSolutionsDemoScript : MonoBehaviour
{
    IMediationManager manager;
    void Start()
    {
        // Initialize CAS
        manager = builder.Initialize();
    }
}

Add AppMetrica prefab on the scene

Open the Assets/AppMetrica folder and drag the prefab AppMetrica to the project's main stage.

If the plugin is integrated this way, the AppMetrica script on the added prefab automatically initializes the AppMetrica SDK and configures tracking of sessions and errors.

⚠️ The added AppMetrica prefab is a singleton. It isn't deleted when switching to a new Unity stage, and it deletes other objects that the AppMetrica script is installed on.

Configure the AppMetrica plugin

In the Inspector panel specify the settings for the prefab added to the scene:

API key — The unique application identifier that is issued in the AppMetrica web interface during app registration.
Exceptions Reporting — Enable or disable sending errors.
Session Timeout Sec — The timeout for ending the session if the app is inactive.
Location Tracking — Enable or disable transmitting location data.
Logs — Enable or disable logging the library's activity.
Handle First Activation As Update — Enable or disable the ability to confirm that the first launch of an app with the AppMetrica Plugin is an app update, and not an install.
StatisticsSending — Enable or disable sending statistics.

Track Impression Level Data

Android

Use our Ad Content Callback for tracking your Impression Level Data.

class MyActivity implements AdCallback {
  MediationManager manager; 
  CASBannerView banner;

  void createBanner() {
      banner = new CASBannerView(this, manager);
      banner.setListener(this);
  }

  void showInterstitial() {
      manager.showInterstitial(MyActivity.this, this);
  }

  void showRewarded() {
      manager.showRewarded(MyActivity.this, this);
  }

  @Override
  void onShown(AdStatusHandler ad) {
     // Executed when the ad is begin displayed.
     // AdStatusHandler is information of ad impression.

     Map<String, Object> eventParameters = new HashMap<String, Object>();
     eventParameters.put("Network", ad.getNetwork());
     eventParameters.put("AdType", ad.getAdType().name());
     
     if(ad.getPriceAccuracy() != PriceAccuracy.UNDISCLOSED) {
         eventParameters.put("Revenue", ad.getCpm() / 1000);
         eventParameters.put("PriceAccuracy", 
              ad.getPriceAccuracy() == PriceAccuracy.FLOOR ? "FLOOR" : "BID");
     } else { 
         eventParameters.put("PriceAccuracy", "UNDISCLOSED");
     }
    
     YandexMetrica.reportEvent("AdImpression", eventParameters);
  }
}
iOS

Use our Ad Content Callback for tracking your Impression Level Data.

class AdExample : UIViewController, CASCallback {
    let manager: CASMediationManager
    @IBOutlet var bannerView: CASBannerView!
    
    func createBanner() {
        bannerView.rootViewController = self
        bannerView.delegate = self
    }
    
    func showInterstitial() {
        manager.presentInterstitial(fromRootViewController: self, callback: self)
    }
    
    func showRewarded() {
        manager.presentRewardedAd(fromRootViewController: self, callback: self)
    }

    func willShown(ad adStatus: CASStatusHandler) {
        var params : [String : Any] = ["AdType": adStatus.adType.description, "Network": adStatus.network]
        
        if (adStatus.priceAccuracy != CASPriceAccuracy.undisclosed) {
            params["Revenue"] = adStatus.cpm / 1000
            
            if (adStatus.priceAccuracy == CASPriceAccuracy.bid) {
                params["PriceAccuracy"] = "BID"
            } else {
                params["PriceAccuracy"] = "FLOOR"
            }
        } else {
            params["PriceAccuracy"] = "UNDISCLOSED"
        }
        
        YMMYandexMetrica.reportEvent("AdImpression", parameters: params);
    }
}
Unity3d

Use our Ad Content Callback for tracking your Impression Level Data.

class CleverAdsSolutionsDemoScript : MonoBehaviour
{
    IMediationManager manager;
    
    void Start()  
    {
        manager.OnBannerAdOpening += onAdOpening;
        manager.OnInterstitialAdOpening += onAdOpening;
        manager.OnRewardedAdOpening += onAdOpening;
    }

    void showInterstitial()
    {
        manager.ShowAd( AdType.Interstitial );
    }

    void showRewarded()
    {
        manager.ShowAd( AdType.Rewarded );
    } 

    void showBanner()
    {
        manager.ShowAd( AdType.Banner );
    }
     
    void onAdOpening(AdMetaData adMetaData)
    {
        // Executed when the ad is begin displayed.
        // AdStatusHandler is information of ad impression.
        var metrica = AppMetrica.Instance;
        Dictionary<string, object> impressionData = new Dictionary<string, object>();
        
        impressionData.Add("Network", adMetaData.network.ToString());
        impressionData.Add("AdType", adMetaData.type.ToString());

        if(adMetaData.priceAccuracy != PriceAccuracy.Undisclosed) {
            impressionData.Add( "Revenue", adMetaData.cpm / 1000 );
            impressionData.Add( "PriceAccuracy", 
                  adMetaData.priceAccuracy == PriceAccuracy.Floor ? "Floor" : "Bid");
        } else { 
             impressionData.Add( "PriceAccuracy", "Undisclosed");
        }
    
        metrica.ReportEvent("AdImpression", impressionData);
    }
}
⚠️ **GitHub.com Fallback** ⚠️