Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ad lifecycle reporting for ads #61

Merged
merged 5 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added
- Ad analytics for ad event reporting

### Changed
- Updated Bitmovin Player to `3.69.0`
- Updated IMA SDK to `3.31.0`
Expand All @@ -13,6 +16,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Updated Gradle wrapper to `8.2` and AGP to `8.2.2`
- Increased minimum required `compileSdk` version to `34`
- Increased `compileSdk` and `targetSdkVersion` to `34`
- Ad break started and ended is now reported in `PlayerEvent.AdBreakStarted` and `PlayerEvent.AdBreakFinished`

### Removed
- Custom event for `AdSkipped` and `AdError`. Replaced by Conviva build in tracking

### Fixed
- The pom file now also includes the `com.bitmovin.player` dependency which was missing before
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import android.util.Log;

import com.bitmovin.player.api.Player;
import com.bitmovin.player.api.advertising.Ad;
import com.bitmovin.player.api.advertising.vast.VastAdData;
import com.bitmovin.player.api.event.Event;
import com.bitmovin.player.api.event.EventListener;
import com.bitmovin.player.api.event.PlayerEvent;
Expand Down Expand Up @@ -34,7 +36,6 @@ public class ConvivaAnalyticsIntegration {
private final BitmovinPlayerHelper playerHelper;

// Helper
private Boolean adStarted = false;
private Boolean isSessionActive = false;
private Boolean isBumper = false;
private Boolean isBackgrounded = false;
Expand Down Expand Up @@ -270,6 +271,7 @@ private void setupPlayerStateManager() {
playerInfo.put(ConvivaSdkConstants.FRAMEWORK_NAME, "Bitmovin Player Android");
playerInfo.put(ConvivaSdkConstants.FRAMEWORK_VERSION, playerHelper.getSdkVersionString());
convivaVideoAnalytics.setPlayerInfo(playerInfo);
convivaAdAnalytics.setAdPlayerInfo(playerInfo);
}

private void internalInitializeSession() {
Expand Down Expand Up @@ -370,6 +372,8 @@ private void attachBitmovinEventListeners() {
bitmovinPlayer.on(PlayerEvent.TimeShifted.class, onTimeShiftedListener);

// Ad events
bitmovinPlayer.on(PlayerEvent.AdBreakStarted.class, onAdBreakStarted);
bitmovinPlayer.on(PlayerEvent.AdBreakFinished.class, onAdBreakFinished);
bitmovinPlayer.on(PlayerEvent.AdStarted.class, onAdStartedListener);
bitmovinPlayer.on(PlayerEvent.AdFinished.class, onAdFinishedListener);
bitmovinPlayer.on(PlayerEvent.AdSkipped.class, onAdSkippedListener);
Expand Down Expand Up @@ -406,6 +410,8 @@ private void detachBitmovinEventListeners() {
bitmovinPlayer.off(PlayerEvent.TimeShifted.class, onTimeShiftedListener);

// Ad events
bitmovinPlayer.off(PlayerEvent.AdBreakStarted.class, onAdBreakStarted);
bitmovinPlayer.off(PlayerEvent.AdBreakFinished.class, onAdBreakFinished);
bitmovinPlayer.off(PlayerEvent.AdStarted.class, onAdStartedListener);
bitmovinPlayer.off(PlayerEvent.AdFinished.class, onAdFinishedListener);
bitmovinPlayer.off(PlayerEvent.AdSkipped.class, onAdSkippedListener);
Expand All @@ -421,20 +427,6 @@ private synchronized void transitionState(ConvivaSdkConstants.PlayerState state)
convivaVideoAnalytics.reportPlaybackMetric(ConvivaSdkConstants.PLAYBACK.PLAYER_STATE, state);
}

// region Helper

private void trackAdEnd() {
if (!adStarted) {
// Do not track adEnd if no ad is was shown (possible if an error occurred)
return;
}
adStarted = false;

Log.d(TAG, "Report ad break ended");
convivaVideoAnalytics.reportAdBreakEnded();
}
// endregion

// region Listeners
private final EventListener<SourceEvent.Unloaded> onSourceUnloadedListener = event -> {
// The default SDK error handling is that it triggers the onSourceUnloaded before the onError event.
Expand Down Expand Up @@ -596,31 +588,84 @@ public void setSeekEnd() {
// endregion

// region Ad events

private final EventListener<PlayerEvent.AdBreakStarted> onAdBreakStarted = new EventListener<PlayerEvent.AdBreakStarted>() {
@Override
public void onEvent(PlayerEvent.AdBreakStarted adBreakStarted) {
Log.d(TAG, "[Player Event] AdBreakStarted");
// For pre-roll ads there is no `PlayerEvent.Play` before the `PlayerEvent.AdBreakStarted`
// which means we need to make sure the session is correctly initialized.
ensureConvivaSessionIsCreatedAndInitialized();
convivaVideoAnalytics.reportAdBreakStarted(ConvivaSdkConstants.AdPlayer.CONTENT, ConvivaSdkConstants.AdType.CLIENT_SIDE);
}
};

private final EventListener<PlayerEvent.AdBreakFinished> onAdBreakFinished = new EventListener<PlayerEvent.AdBreakFinished>() {
@Override
public void onEvent(PlayerEvent.AdBreakFinished adBreakFinished) {
Log.d(TAG, "[Player Event] AdBreakFinished");
convivaVideoAnalytics.reportAdBreakEnded();
}
};

private final EventListener<PlayerEvent.AdStarted> onAdStartedListener = new EventListener<PlayerEvent.AdStarted>() {
@Override
public void onEvent(PlayerEvent.AdStarted adStartedEvent) {
Log.d(TAG, "[Player Event] AdStarted");
adStarted = true;
convivaVideoAnalytics.reportAdBreakStarted(ConvivaSdkConstants.AdPlayer.CONTENT, ConvivaSdkConstants.AdType.CLIENT_SIDE);
Map<String, Object> adInfo = null;
if (adStartedEvent.getAd() != null) {
adInfo = adToAdInfo(adStartedEvent.getAd());
}
convivaAdAnalytics.reportAdLoaded(adInfo);
convivaAdAnalytics.reportAdStarted(adInfo);
convivaAdAnalytics.reportAdMetric(ConvivaSdkConstants.PLAYBACK.PLAYER_STATE, ConvivaSdkConstants.PlayerState.PLAYING);
}
};

private final EventListener<PlayerEvent.AdFinished> onAdFinishedListener = adFinishedEvent -> {
Log.d(TAG, "[Player Event] AdFinished");
trackAdEnd();
private Map<String, Object> adToAdInfo(Ad ad) {
Map<String, Object> adInfo = new HashMap<>();
adInfo.put("c3.ad.technology", "Client Side");

if (ad.getMediaFileUrl() != null) {
adInfo.put(ConvivaSdkConstants.STREAM_URL, ad.getMediaFileUrl());
}
if (ad.getId() != null) {
adInfo.put("c3.ad.id", ad.getId());
}

if (ad.getData() instanceof VastAdData) {
VastAdData vastAdData = (VastAdData) ad.getData();
if (vastAdData.getAdTitle() != null) {
adInfo.put(ConvivaSdkConstants.ASSET_NAME, vastAdData.getAdTitle());
}
}
return adInfo;
}

private final EventListener<PlayerEvent.AdFinished> onAdFinishedListener = new EventListener<PlayerEvent.AdFinished>() {
@Override
public void onEvent(PlayerEvent.AdFinished adFinished) {
Log.d(TAG, "[Player Event] AdFinished");
convivaAdAnalytics.reportAdEnded();
}
};

private final EventListener<PlayerEvent.AdSkipped> onAdSkippedListener = adSkippedEvent -> {
Log.d(TAG, "[Player Event] AdSkipped");
customEvent(adSkippedEvent);
trackAdEnd();
private final EventListener<PlayerEvent.AdSkipped> onAdSkippedListener = new EventListener<PlayerEvent.AdSkipped>() {
@Override
public void onEvent(PlayerEvent.AdSkipped adSkipped) {
Log.d(TAG, "[Player Event] AdSkipped");
convivaAdAnalytics.reportAdSkipped();
}
};

private final EventListener<PlayerEvent.AdError> onAdErrorListener = adErrorEvent -> {
Log.d(TAG, "[Player Event] AdError");
customEvent(adErrorEvent);
trackAdEnd();
private final EventListener<PlayerEvent.AdError> onAdErrorListener = new EventListener<PlayerEvent.AdError>() {
@Override
public void onEvent(PlayerEvent.AdError adError) {
Log.d(TAG, "[Player Event] AdError");
convivaAdAnalytics.reportAdFailed(adError.getMessage());
}
};

// endregion

private final EventListener<PlayerEvent.VideoPlaybackQualityChanged> onVideoPlaybackQualityChangedListener = videoPlaybackQualityChangedEvent -> {
Expand Down