Initialize CAS - cleveradssolutions/CAS-Flutter GitHub Wiki

To begin, create a string constant with your casId. This constant will be needed when creating each ad format instance.

If you haven't created an CAS account and registered an app yet, now's a great time to do so at https://cas.ai.

  • In a real app, it is important that you use your actual CAS ID.
  • If you just want to experiment or test the SDK, you can skip the configuration your project.
What is CAS ID?

In most cases, a casId is the same as your Android application package name or your App Store ID. You can choose one, register and use it.

  • Android
    • A package name uniquely identifies your app on the device and in the Google Play Store.
    • The package name value is case-sensitive and is often referred to as an APPLICATION_ID.
    • Find your app's package name in your module (app-level) Gradle file, usually android/app/build.gradle (example package name: com.yourcompany.yourproject).
  • iOS
    • You can find an app store ID in the URL of your app’s Apple App Store URL. For example, the URL is apps.apple.com/us/app/id123456789 then app store ID is 123456789.
import 'package:clever_ads_solutions/clever_ads_solutions.dart';

const String _casId = 'demo';

class _HomeScreenState extends State<HomeScreen> implements OnDismissListener {

  @override
  void initState() {
    super.initState();
    _initialize();
  }
}

You can skip the manual call for ad initialization, and then the SDK will automatically perform the initialization before the first ad load. However, if it's important for you to change the configuration or listen to states of the Consent Flow, make sure to initialize it at least once before the first ad load.

void _initialize() {
  final builder = CAS
      .buildManager()
      .withCasId(_casId)
      .withCompletionListener(_onCASInitialized);

  builder.build();
}

void _onCASInitialized(InitConfig initConfig) {
  // The CAS SDK initializes if the error is `null`
  String? initErrorOrNull = initConfig.error;
  String? userCountryISO2OrNull = initConfig.countryCode;

  // True if the user is protected by GDPR or other regulations
  bool protectionApplied = initConfig.isConsentRequired;
}

The onCASInitialized listener may be called with an error. In this case, the SDK will attempt to reinitialize and the listener will be called again until the error is resolved.

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 initialize CAS SDK. When the user completes the flow, the SDK calls your initialization-completion handler.

CAS consent flow is enabled by default. You can disable the consent flow by add disabled ConsentFlow instance to withConsentFlow():

builder.withConsentFlow(
  ConsentFlow.create()
);

Make sure to apply all configurations before calling builder.build().

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 call initializeCAS() with the following option:

builder.withTestMode(kDebugMode);

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

Legacy support MediationManager

We are not removing support for managing ads using MediationManager at this time, but we recommend migrating to the new CASInterstitial and CASRewarded components whenever possible.

To continue using MediationManager, you still need to specify the ad formats you want to work with, just as before.

builder.withAdTypes(AdTypeFlags.interstitial | AdTypeFlags.rewarded);

If your app no longer uses MediationManager for Interstitial and Rewarded formats, simply do not define any formats during initialization.

Retrieve the Version Number

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

final String sdkVersion = await CAS.getSDKVersion();

(Optional) Trial ad-free interval

Set the time interval during which users can enjoy an ad-free experience while retaining access to Rewarded Ads and App Open Ads formats. This interval is defined from the moment of the initial app installation, in seconds. Within this interval, users enjoy privileged access to the application's features without intrusive advertisements.

int secondsIn7Days = 604800;
CAS.settings.setTrialAdFreeInterval(secondsIn7Days);

🔗 Next
Privacy Regulations

⚠️ **GitHub.com Fallback** ⚠️