Skip to content

Commit

Permalink
dispatcher: refactor dispatcher to handle both piano sdks
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaud-roland committed Dec 5, 2023
1 parent 8332f9f commit 5d9a89a
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* If you want to dispatch as custom event, please see {@link LegacyPianoDispatcher#enableBatchCustomEvents(boolean)}.
* Note: if you enable custom events, you need to declare them in your Piano Data Model.
*/
public abstract class PianoDispatcher implements BatchEventDispatcher {
public abstract class AbstractPianoDispatcher implements BatchEventDispatcher {

/**
* Batch internal dispatcher information used for analytics
Expand Down Expand Up @@ -158,7 +158,7 @@ public int getVersion() {
* First, check for an "at_campaign" tag in the custom payload, or in the deeplink.
* If not, check for an "utm_campaign" tag in the custom payload, or in the deeplink.
* If not, check if there is a TrackingID attached.
* If not, use {@link PianoDispatcher#BATCH_DEFAULT_CAMPAIGN}
* If not, use {@link AbstractPianoDispatcher#BATCH_DEFAULT_CAMPAIGN}
*
* @param payload Batch event payload
* @return The campaign label.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.batch.android.dispatcher.piano

import androidx.annotation.VisibleForTesting
import com.batch.android.Batch
import com.batch.android.Batch.EventDispatcher.Payload
import io.piano.android.analytics.PianoAnalytics
import io.piano.android.analytics.model.Event
import io.piano.android.analytics.model.Property
import io.piano.android.analytics.model.PropertyName

/**
* Piano Event Dispatcher (Kotlin)
* Instantiated when running on Piano SDK 3.3.0 or newer.
*/
class KtPianoDispatcher() : AbstractPianoDispatcher() {

/**
* Callback fired when a new Batch event is triggered
*
* @param type The type of the event
* @param payload The associated payload of the event
*/
override fun dispatchEvent(type: Batch.EventDispatcher.Type, payload: Payload) {
// Dispatch onSiteAds event
if (onSiteAdsEventsEnabled && shouldBeDispatchedAsOnSiteAd(type)) {
buildPianoOnSiteAdsEvent(type, payload)?.let {
PianoAnalytics.getInstance().sendEvents(it)
}
}
// Dispatch Custom Event if enabled
if (customEventsEnabled) {
val event: Event = buildPianoCustomEvent(type, payload)
PianoAnalytics.getInstance().sendEvents(event)
}
}

/**
* Build an On-Site Ads Piano Event from a Batch Event
*
* @param type Batch event type
* @param payload Batch event payload
* @return The Piano event to send
*/
@VisibleForTesting
fun buildPianoOnSiteAdsEvent(type: Batch.EventDispatcher.Type, payload: Payload): Event? {
val pianoOnSiteEventName: String = if (isImpression(type)) {
EVENT_IMPRESSION
} else if (isClick(type)) {
EVENT_CLICK
} else {
return null
}
return Event.Builder(pianoOnSiteEventName).properties(
Property(PropertyName(ON_SITE_TYPE), ON_SITE_TYPE_PUBLISHER),
Property(PropertyName(ON_SITE_ADVERTISER), getSource(payload)),
Property(PropertyName(ON_SITE_CAMPAIGN), getCampaign(payload)),
Property(PropertyName(ON_SITE_FORMAT), getMedium(payload, type)),
).build()
}

@VisibleForTesting
fun buildPianoCustomEvent(
type: Batch.EventDispatcher.Type,
payload: Payload
): Event {
val name = getPianoEventName(type)
val data: MutableSet<Property> = mutableSetOf(
Property(PropertyName(ON_SITE_TYPE), ON_SITE_TYPE_PUBLISHER),
Property(PropertyName(SOURCE), getSource(payload)),
Property(PropertyName(CAMPAIGN), getCampaign(payload)),
Property(PropertyName(MEDIUM), getMedium(payload, type)),
Property(PropertyName(SOURCE_FORCE), true)
)
with(data) {
payload.trackingId?.let {
if (it.isNotBlank()) {
add(Property(PropertyName(BATCH_TRACKING_ID), it))
}
}
getContent(payload)?.let {
if (it.isNotBlank()) {
add(Property(PropertyName(CONTENT), it))
}
}
if (type.isMessagingEvent) {
payload.webViewAnalyticsID?.let {
if (it.isNotBlank()) {
add(Property(PropertyName(BATCH_WEBVIEW_ANALYTICS_ID), it))
}
}
}
}
return Event.Builder(name).properties(data).build()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* Legacy Piano Event Dispatcher
* Instantiated when running on Piano SDK 3.2.1 or older.
*/
public class LegacyPianoDispatcher extends PianoDispatcher {
public class LegacyPianoDispatcher extends AbstractPianoDispatcher {

/**
* Piano Analytics instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public class PianoRegistrar implements DispatcherRegistrar {

private static PianoDispatcher instance = null;
private static AbstractPianoDispatcher instance = null;

/**
* Meta-data name to enable custom events
Expand All @@ -30,13 +30,12 @@ public class PianoRegistrar implements DispatcherRegistrar {
@Override
public BatchEventDispatcher getDispatcher(Context context) {
if (instance == null) {
// Abort initialization when the App is running on the new Piano SDK.
if (isNewPianoSDKPresent()) {
Log.e("Batch", "PianoDispatcher - It looks like you are using an unsupported version of the Piano SDK." +
" This dispatcher requires version 3.2.1 or older. Aborting initialization.");
return null;
instance = new KtPianoDispatcher();
} else {
Log.w("Batch", "PianoDispatcher - It looks like your app is running with an old version of the Piano Analytics SDK. You should migrate on version 3.3.0 or newer.");
instance = new LegacyPianoDispatcher(context);
}
instance = new LegacyPianoDispatcher(context);
instance.enableBatchCustomEvents(getBooleanMetaDataInfo(context, CUSTOM_EVENT_ENABLED_METADATA, false));
instance.enableBatchOnSiteAdsEvents(getBooleanMetaDataInfo(context, ONSITE_AD_EVENT_ENABLED_METADATA, true));
instance.enableUTMTracking(getBooleanMetaDataInfo(context, UTM_TRACKING_ENABLED_METADATA, true));
Expand Down

0 comments on commit 5d9a89a

Please sign in to comment.