Initialize CAS - cleveradssolutions/CAS-Flutter GitHub Wiki

Before loading ads, have your app initialize CAS Mobile Ads SDK by calling CASMobileAds.initialize() which initializes the SDK and returns a Future that finishes once initialization is complete.

The casId is a unique identifier for the CAS content registered for your app on each platform.
You can use the value demo to force test ads mode on all devices if you haven't registered a casId yet.

import 'package:clever_ads_solutions/clever_ads_solutions.dart';
  
void main() {  

  String casId = Platform.isAndroid ? 'demo' : 'demo';
  
  CASMobileAds.initialize(  
    casId: casId,  
  ).then((InitializationStatus status) {  
    if (status.error != null) {  
      print('CAS Mobile Ads SDK initialization failed: ${status.error}');  
    } else {  
      print('CAS Mobile Ads SDK initialized in ${status.countryCode} country');  
    }  
  });  
  
  runApp(const MyApp());  
}

If initialize() method is not called, the first ad load automatically initializes the CAS Mobile Ads SDK with CAS Id defined in ad load arguments.

If an error occurs, the SDK will attempt automatic reinitialization internally. However, the Future will not be updated with subsequent InitializationStatus. For the most up-to-date InitializationStatus, call initialize() method again at a later time.

[!IMPORTANT]
Do not initialize mediated advertising SDKs (CAS does that for you).
Not following this step will result in noticeable integration issues.

Automatic user consent flow

To get consent for collecting personal data of your users, we suggest you use a built-in Consent Flow, comes with a pre-made consent form that you can easily present to your users. That means you no longer need to create your own consent window.

The user will see the consent flow when your app initializes the CAS SDK.
Once the user completes the flow, the initialize Future will complete.

You can choose whether to showConsentFormIfRequired in the arguments of the initialize() method:

  CASMobileAds.initialize(  
    casId: casId,  
    showConsentFormIfRequired: true,
    ...
  ).then((InitializationStatus status) {  
    if (status.consentFlowStatus == ConsentFlow.statusObtained) {  
      print('CAS Mobile Ads SDK obtain user consent');  
    } 
  });  

You must wait until the user finishes the consent flow before you initialize third-party SDKs (such as MMPs or analytics SDKs). For this reason, initialize such SDKs from within your initialization-completion callback. If you were to initialize these third-party SDKs before the user completes the consent flow, these third-party SDKs would not be able to access relevant identifiers and you would suffer a material impact on measurement, reporting, and ad revenue.

[!NOTE]
Read more about "Privacy options button" and "Debug geography" on CAS User Consent Flow page

Always test with test ads

When building and testing your apps, make sure you use test ads rather than live, production ads. Failure to do so can lead to suspension of your account.

By default, CAS initializes mediation in live mode. To enable test ad mode, you should manually set forceTestAds in the arguments of the initialize() method:

  CASMobileAds.initialize(  
    casId: casId,  
    forceTestAds: kDebugMode || kProfileMode,
    testDeviceIds: [
        'Your test device ID'
    ],
    ...
  )

You can also test in release build by registering your device as a test device. Check the logs for your device's ID value.

For more information about how the CAS.AI SDK's test ads work, see Enable test ads

Prohibition on Personal Information from Children

Apps can be marked as child-directed or Children’s Online Privacy Protection Act (COPPA) applicable through the CAS UI.

If the app has knowledge of specific individuals as being COPPA-applicable, it should make use of the targetAudienceparameter in the initialize() function.

  CASMobileAds.initialize(  
    casId: casId,  
    audience: Audience.notChildren,
    ...
  )

If your app targets both children and older users, all ads that may be shown to children must comply with COPPA.
To ensure compliance, a neutral age screen must be implemented so that ads unsuitable for children are only shown to appropriate audiences.
A neutral age screen is a tool—such as an age gate—that verifies a user’s age without encouraging them to misrepresent it, and prevents children from accessing content not intended for them.

(Optional) Retrieve the Version Number

To programmatically retrieve the SDK version number at runtime, CAS provides the following method:

String sdkVersion = await CAS.getSDKVersion();

(Optional) Trial ad-free interval

Defines the time interval, in seconds, starting from the moment of the initial app installation, during which users can use the application without ads being displayed while still retaining access to the Rewarded Ads format. Within this interval, users enjoy privileged access to the application's features without intrusive advertisements.

int secondsIn7Days = 604800;
CASMobileAds.setTrialAdFreeInterval(secondsIn7Days);

Complete example


🔗 Next