-
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: refactoring dispatcher to handle new sdk
- Loading branch information
1 parent
3702da2
commit 27ec22d
Showing
5 changed files
with
166 additions
and
148 deletions.
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
piano-dispatcher/src/main/java/com/batch/android/dispatcher/piano/LegacyPianoDispatcher.java
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,127 @@ | ||
package com.batch.android.dispatcher.piano; | ||
|
||
import android.content.Context; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import androidx.annotation.VisibleForTesting; | ||
|
||
import com.batch.android.Batch; | ||
|
||
import java.util.HashMap; | ||
|
||
|
||
import io.piano.analytics.Event; | ||
import io.piano.analytics.PianoAnalytics; | ||
|
||
/** | ||
* Legacy Piano Event Dispatcher | ||
* Instantiated when running on Piano SDK 3.2.1 or older. | ||
*/ | ||
public class LegacyPianoDispatcher extends PianoDispatcher { | ||
|
||
/** | ||
* Piano Analytics instance | ||
*/ | ||
private final PianoAnalytics pianoAnalytics; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param context application context | ||
*/ | ||
public LegacyPianoDispatcher(@NonNull Context context) { | ||
pianoAnalytics = PianoAnalytics.getInstance(context); | ||
} | ||
|
||
/** | ||
* 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 | ||
public void dispatchEvent(@NonNull Batch.EventDispatcher.Type type, @NonNull Batch.EventDispatcher.Payload payload) { | ||
|
||
// Dispatch onSiteAds event | ||
if (onSiteAdsEventsEnabled && shouldBeDispatchedAsOnSiteAd(type)) { | ||
Event onSiteAdsEvent = buildPianoOnSiteAdsEvent(type, payload); | ||
if (onSiteAdsEvent != null) { | ||
pianoAnalytics.sendEvent(onSiteAdsEvent); | ||
} | ||
} | ||
// Dispatch Custom Event if enabled | ||
if (customEventsEnabled) { | ||
Event event = buildPianoCustomEvent(type, payload); | ||
pianoAnalytics.sendEvent(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 | ||
*/ | ||
@Nullable | ||
@VisibleForTesting | ||
Event buildPianoOnSiteAdsEvent(Batch.EventDispatcher.Type type, Batch.EventDispatcher.Payload payload) { | ||
|
||
String pianoOnSiteEventName; | ||
if (isImpression(type)) { | ||
pianoOnSiteEventName = EVENT_IMPRESSION; | ||
} else if (isClick(type)) { | ||
pianoOnSiteEventName = EVENT_CLICK; | ||
} else { | ||
return null; | ||
} | ||
|
||
HashMap<String, Object> params = new HashMap<String, Object>() {{ | ||
put(ON_SITE_TYPE, ON_SITE_TYPE_PUBLISHER); | ||
put(ON_SITE_ADVERTISER, getSource(payload)); | ||
put(ON_SITE_CAMPAIGN, getCampaign(payload)); | ||
put(ON_SITE_FORMAT, getMedium(payload, type)); | ||
}}; | ||
return new Event(pianoOnSiteEventName, params); | ||
} | ||
|
||
/** | ||
* Build a Piano Custom Event from a Batch Event | ||
* | ||
* @param type Batch event type | ||
* @param payload Batch event payload | ||
* @return The Piano event to send | ||
*/ | ||
@VisibleForTesting | ||
Event buildPianoCustomEvent(@NonNull Batch.EventDispatcher.Type type, | ||
@NonNull Batch.EventDispatcher.Payload payload) { | ||
|
||
String eventName = getPianoEventName(type); | ||
HashMap<String, Object> eventData = new HashMap<String, Object>() {{ | ||
put(CAMPAIGN, getCampaign(payload)); | ||
put(MEDIUM, getMedium(payload, type)); | ||
put(SOURCE, getSource(payload)); | ||
put(SOURCE_FORCE, true); | ||
}}; | ||
|
||
String trackingId = payload.getTrackingId(); | ||
if (trackingId != null && !trackingId.isEmpty()) { | ||
eventData.put(BATCH_TRACKING_ID, trackingId); | ||
} | ||
|
||
String content = getContent(payload); | ||
if (content != null && !content.isEmpty()) { | ||
eventData.put(CONTENT, content); | ||
} | ||
|
||
if (type.isMessagingEvent()) { | ||
String webViewAnalyticsId = payload.getWebViewAnalyticsID(); | ||
if (webViewAnalyticsId != null && !webViewAnalyticsId.isEmpty()) { | ||
eventData.put(BATCH_WEBVIEW_ANALYTICS_ID, webViewAnalyticsId); | ||
} | ||
} | ||
return new Event(eventName, eventData); | ||
} | ||
|
||
} |
Oops, something went wrong.