Skip to main content

Google integration

Ad insertionAd trackingBkYouGoogle

If using Google as ad provider, it requires few additional steps.

Requirements

1. Setup Google PAL SDK

The Google PAL SDK has to be included manually (not yet available on the Google official repository).

Find the latest version on the Google developer platform: https://developers.google.com/ad-manager/pal/android/

And add it as a local maven repository to your root build.gradle

dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven {
url "https://delivery-platform.broadpeak.tv/android/repository/all"
}

// Google PAL local repository
maven {
url file("PATH_TO_PAL_SDK/GooglePAL")
}
}
}

Next, add the dependency to the build.gradle file of your app module. We highly recommend to use PAL SDK 17.1.0, please ask us if you need another version.

implementation 'com.google.android.gms:play-services-pal:17.1.0'
implementation 'com.google.android.gms:play-services-tasks:17.1.0'

Finally, add the rule to the proguard file

-keep class com.google.ads.interactivemedia.pal.* { *; }
-keep public interface com.google.ads.interactivemedia.pal.NonceGenerator$NonceGeneratorCallback { *; }
-keep class com.google.ads.interactivemedia.pal.NonceRequest$Builder { *; }
-keep interface com.google.android.gms.tasks.OnSuccessListener { *; }
-keep interface com.google.android.gms.tasks.OnFailureListener { *; }
-keep class com.google.android.gms.tasks.Task { *; }

2. Initialize the Google PAL SDK

First, you need to create a ConsentSettings and pass it to SmartLib. It has to be set before setPalParameters(...). Find more details on the Google PAL documentation.

// The default value for allowStorage() is false, but can be
// changed once the appropriate consent has been gathered. The
// getConsentToStorage() method is a placeholder for the publisher's own
// method of obtaining user consent, either by integrating with a CMP or
// based on other methods the publisher chooses to handle storage consent.

boolean isConsentToStorage = getConsentToStorage();
ConsentSettings consentSettings = ConsentSettings.builder()
.allowStorage(isConsentToStorage)
.build();
AdManager.getInstance().setConsentSettings(consentSettings);

To enable nonce generation, the PAL parameters have to be initialized once, after SmartLib init. If parameters are not yet known, set values to null.

Depending on your Google ad campaign, pal parameters may have to be set before each streaming session with ads.

/**
* Set Google PAL parameters
*
* @param descriptionURL Google PAL parameter, set null if not used
* @param partnerName Google PAL parameter, set null if not used
* @param partnerVersion Google PAL parameter, set null if not used
* @param omidVersion Google PAL parameter, set null if not used
* @param playerType Google PAL parameter, set null if not used
* @param playerVersion Google PAL parameter, set null if not used
* @param ppid Google PAL parameter, set null if not used
* @param videoPlayerHeight Google PAL parameter, set 0 if not used
* @param videoPlayerWidth Google PAL parameter, set 0 if not used
* @param willAdAutoPlay Google PAL parameter, set false if not used
* @param willAdPlayMuted Google PAL parameter, set false if not used
*/
AdManager.getInstance().setPalParameters(descriptionURL, partnerName, partnerVersion,
omidVersion, playerType, playerVersion, ppid,
videoPlayerHeight, videoPlayerWidth, willAdAutoPlay, willAdPlayMuted);

Parameters are reset when SmartLib release is called.

3. Notify Google PAL SDK of user events

note

This step is not necessary on web platforms.

// Send touch when the player view is receiving a MotionEvent object
playerView.setOnTouchListener((view, motionEvent) -> {
session.adTouch(motionEvent);

return false;
});

The sendAdImpression event required by the PAL SDK is handled by SmartLib.

4. Full example

// Init SmartLib and Google PAL
SmartLib.getInstance().init(getApplicationContext(), ..., ..., ...);

// Set consent settings (if using Google)
boolean isConsentToStorage = getConsentToStorage();
ConsentSettings consentSettings = ConsentSettings.builder()
.allowStorage(isConsentToStorage)
.build();
AdManager.getInstance().setConsentSettings(consentSettings);

// Set Google PAL parameters at init (if using Google, to speed up first nonce generation)
AdManager.getInstance().setPalParameters(descriptionURL, partnerName, partnerVersion,
omidVersion, playerType, playerVersion, ppid,
videoPlayerHeight, videoPlayerWidth, willAdAutoPlay, willAdPlayMuted);

// Create a new session
StreamingSession session = SmartLib.getInstance().createStreamingSession();

// Attach the player
session.attachPlayer(player);

// Forward touch events to Google PAL
playerView.setOnTouchListener((view, motionEvent) -> {
session.adTouch(motionEvent);

return false;
});

// Listen to ad events
session.setAdEventsListener(new AdManager.AdEventsListener() {
@Override
public void onAdBreakBegin(AdBreakData adBreak) {
// Lock player controls
mControlsLayout.setClickable(false);
}

@Override
public void onAdBegin(AdData adData, AdBreakData adBreakData) {
// Show ad link button if needed
mAdLink.setVisible(View.VISIBLE);

mAdLink.setClickURL(clickURL); // on click, it will call session.adUserInteraction(AdInteractionType.CLICK);
}

@Override
public void onAdSkippable(AdData adData, AdBreakData adBreakData, long adSkippablePosition, long adEndPosition, long adBreakEndPosition) {
// Show the skip message/button "skip ad in x seconds"
mAdSkipMessage.setVisibility(View.VISIBLE);
}

@Override
public void onAdEnd(AdData adData, AdBreakData adBreakData) {
// Hide ad link and ad skip button
mAdLink.setVisible(View.GONE);
mAdSkipMessage.setVisibility(View.GONE);
}

@Override
public void onAdBreakEnd(AdBreakData adBreakData) {
// Hide ad link and ad skip button
mAdLink.setVisible(View.GONE);
mAdSkipMessage.setVisibility(View.GONE);

// Unlock player controls
mControlsLayout.setClickable(true);
}
});

...

// Start the session with/without ads
if (contentContainsAds) { // To be adapted
session.activateAdvertising();

// Set ad parameters
session.setAdParameter("name1", "value1");
session.setAdParameter("name2", "value2");
}

// You must not call getURL on the main thread !
// Because of generation duration of the nonce and technical requirements, it has to be done on another thread
StreamingSessionResult result = session.getURL(CONTENT_URL);

...

// Notify that the user opened the ad link
session.adUserInteraction(AdInteractionType.CLICK);

// Stop the session
session.stopStreamingSession();

...