-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dispatcher: refactor dispatcher to handle both piano sdks
- Loading branch information
1 parent
8332f9f
commit 5d9a89a
Showing
4 changed files
with
103 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
piano-dispatcher/src/main/java/com/batch/android/dispatcher/piano/KtPianoDispatcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters