Skip to content

Commit

Permalink
dispatcher: add support for Batch WebView In-App format
Browse files Browse the repository at this point in the history
  • Loading branch information
abarisain committed Mar 8, 2021
1 parent d169b74 commit e8a6af9
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 5 deletions.
2 changes: 1 addition & 1 deletion atinternet-dispatcher/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ android {
}

dependencies {
api 'com.batch.android:batch-sdk:1.15.0'
api 'com.batch.android:batch-sdk:1.17.0'
api "com.atinternet:Tracker:$atInternetVersion"
implementation "androidx.annotation:annotation:$androidXLibraryVersion"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import androidx.annotation.Nullable;

import com.atinternet.tracker.ATInternet;
import com.atinternet.tracker.CustomVar;
import com.atinternet.tracker.Publisher;
import com.atinternet.tracker.Screen;
import com.atinternet.tracker.Tracker;
Expand Down Expand Up @@ -37,8 +38,10 @@ public class AtInternetDispatcher implements BatchEventDispatcher
private static final String NOTIFICATION_DISMISS_NAME = "DismissedBatchPushNotification";
private static final String MESSAGING_SHOW_NAME = "ShowedBatchInAppMessage";
private static final String MESSAGING_CLOSE_NAME = "ClosedBatchInAppMessage";
private static final String MESSAGING_CLOSE_ERROR_NAME = "ClosedErrorBatchInAppMessage";
private static final String MESSAGING_AUTO_CLOSE_NAME = "AutoClosedBatchInAppMessage";
private static final String MESSAGING_CLICK_NAME = "ClickedBatchInAppMessage";
private static final String MESSAGING_WEBVIEW_CLICK_NAME = "WebViewClickedBatchInAppMessage";
private static final String UNKNOWN_EVENT_NAME = "UnknownBatchMessage";

private final Map<String, Tracker> trackerCache;
Expand Down Expand Up @@ -70,8 +73,12 @@ public void dispatchEvent(@NonNull Batch.EventDispatcher.Type type,
if (xtorTag != null) {
screen.Campaign(xtorTag);
}
screen.sendView();

String webViewButtonId = payload.getWebViewAnalyticsID();
if (webViewButtonId != null) {
screen.CustomVars().add(1, webViewButtonId, CustomVar.CustomVarType.Screen);
}
screen.sendView();
}

/**
Expand Down Expand Up @@ -137,9 +144,16 @@ private void dispatchAsOnSiteAd(Batch.EventDispatcher.Type type, Batch.EventDisp
}
publisher.setAdvertiserId("[batch]");

String webViewButtonId = payload.getWebViewAnalyticsID();
if (webViewButtonId != null) {
publisher.setVariant("[" + webViewButtonId + "]");
}

if (isImpression(type)) {
publisher.sendImpression();
} else if (isClick(type) && payload.isPositiveAction()) {
} else if (isClick(type) &&
(payload.isPositiveAction() || type == Batch.EventDispatcher.Type.MESSAGING_WEBVIEW_CLICK)) {
// We send the click if it's a positive action or if it's a click inside a WebView In-App
publisher.sendTouch();
}
}
Expand Down Expand Up @@ -200,7 +214,8 @@ private static boolean isImpression(Batch.EventDispatcher.Type type) {

private static boolean isClick(Batch.EventDispatcher.Type type) {
return type.equals(Batch.EventDispatcher.Type.NOTIFICATION_OPEN) ||
type.equals(Batch.EventDispatcher.Type.MESSAGING_CLICK);
type.equals(Batch.EventDispatcher.Type.MESSAGING_CLICK) ||
type.equals(Batch.EventDispatcher.Type.MESSAGING_WEBVIEW_CLICK);
}

private static Map<String, String> getFragmentMap(String fragment)
Expand Down Expand Up @@ -230,8 +245,12 @@ private static String getATEventName(Batch.EventDispatcher.Type type) {
return MESSAGING_CLOSE_NAME;
case MESSAGING_AUTO_CLOSE:
return MESSAGING_AUTO_CLOSE_NAME;
case MESSAGING_CLOSE_ERROR:
return MESSAGING_CLOSE_ERROR_NAME;
case MESSAGING_CLICK:
return MESSAGING_CLICK_NAME;
case MESSAGING_WEBVIEW_CLICK:
return MESSAGING_WEBVIEW_CLICK_NAME;
}
return UNKNOWN_EVENT_NAME;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.os.Bundle;

import com.atinternet.tracker.ATInternet;
import com.atinternet.tracker.CustomVar;
import com.atinternet.tracker.CustomVars;
import com.atinternet.tracker.Publisher;
import com.atinternet.tracker.Publishers;
import com.atinternet.tracker.Screen;
Expand Down Expand Up @@ -74,6 +76,7 @@ public void testNotificationDisplay() {
Mockito.when(screens.add("DisplayedBatchPushNotification")).thenReturn(screen);

TestEventPayload payload = new TestEventPayload(null,
null,
null,
new Bundle());

Expand Down Expand Up @@ -103,6 +106,7 @@ public void testNotificationDisplayCampaignLabelFragment() {
Mockito.when(screens.add("DisplayedBatchPushNotification")).thenReturn(screen);

TestEventPayload payload = new TestEventPayload(null,
null,
"https://batch.com.com/test#xtor=" + xtor,
new Bundle());

Expand Down Expand Up @@ -133,6 +137,7 @@ public void testNotificationDisplayCampaignLabelFragmentEncode() {
Mockito.when(screens.add("DisplayedBatchPushNotification")).thenReturn(screen);

TestEventPayload payload = new TestEventPayload(null,
null,
"https://batch.com/test#xtor=" + xtor,
new Bundle());

Expand Down Expand Up @@ -162,6 +167,7 @@ public void testNotificationDisplayHostLessDeeplinkQuery() {
Mockito.when(screens.add("DisplayedBatchPushNotification")).thenReturn(screen);

TestEventPayload payload = new TestEventPayload(null,
null,
"batch://?xtor=" + xtor,
new Bundle());

Expand Down Expand Up @@ -191,6 +197,7 @@ public void testNotificationDisplayHostLessDeeplinkFragment() {
Mockito.when(screens.add("DisplayedBatchPushNotification")).thenReturn(screen);

TestEventPayload payload = new TestEventPayload(null,
null,
"batch://#xtor=" + xtor,
new Bundle());

Expand Down Expand Up @@ -220,6 +227,7 @@ public void testNotificationOpenCampaignLabelQuery() {
Mockito.when(screens.add("OpenedBatchPushNotification")).thenReturn(screen);

TestEventPayload payload = new TestEventPayload(null,
null,
"https://batch.com/test?xtor=" + xtor,
new Bundle(), true);

Expand Down Expand Up @@ -250,6 +258,7 @@ public void testNotificationOpenCampaignLabelQueryEncode() {
Mockito.when(screens.add("OpenedBatchPushNotification")).thenReturn(screen);

TestEventPayload payload = new TestEventPayload(null,
null,
"https://batch.com/test?xtor=" + xtor,
new Bundle(), true);

Expand Down Expand Up @@ -281,6 +290,7 @@ public void testNotificationOpenCampaignLabelCustomPayload() {
Bundle customPayload = new Bundle();
customPayload.putString("xtor", xtor);
TestEventPayload payload = new TestEventPayload(null,
null,
null,
customPayload, true);

Expand Down Expand Up @@ -311,6 +321,7 @@ public void testNotificationOpenCampaignLabelTrackingID() {

Bundle customPayload = new Bundle();
TestEventPayload payload = new TestEventPayload(xtor,
null,
null,
customPayload, true);

Expand Down Expand Up @@ -338,6 +349,7 @@ public void testNotificationOpenNonPositive() {

Bundle customPayload = new Bundle();
TestEventPayload payload = new TestEventPayload(null,
null,
null,
customPayload, false);

Expand Down Expand Up @@ -365,6 +377,7 @@ public void testNotificationOpenCampaignLabelPriority() {
Bundle customPayload = new Bundle();
customPayload.putString("xtor", xtor);
TestEventPayload payload = new TestEventPayload(null,
null,
"https://batch.com/test?xtor=AD-[fake]#xtor=CS8-[fake2]",
customPayload, true);

Expand Down Expand Up @@ -394,6 +407,7 @@ public void testNotificationOpenCampaignLabelNonTrimmed() {

Bundle customPayload = new Bundle();
TestEventPayload payload = new TestEventPayload(null,
null,
" \n https://batch.com/test?xtor=AD-[fake] \n ",
customPayload, true);

Expand All @@ -420,6 +434,7 @@ public void testNotificationOpen() {
Mockito.when(screens.add("OpenedBatchPushNotification")).thenReturn(screen);

TestEventPayload payload = new TestEventPayload(null,
null,
null,
new Bundle(), true);

Expand All @@ -446,6 +461,7 @@ public void testNotificationDismiss() {
Mockito.when(screens.add("DismissedBatchPushNotification")).thenReturn(screen);

TestEventPayload payload = new TestEventPayload(null,
null,
null,
new Bundle(), true);

Expand All @@ -468,6 +484,7 @@ public void testInAppShow() {
Mockito.when(screens.add("ShowedBatchInAppMessage")).thenReturn(screen);

TestEventPayload payload = new TestEventPayload(null,
null,
null,
new Bundle());

Expand Down Expand Up @@ -495,6 +512,7 @@ public void testInAppShowCampaignId() {
Mockito.when(screens.add("ShowedBatchInAppMessage")).thenReturn(screen);

TestEventPayload payload = new TestEventPayload(xtor,
null,
null,
new Bundle());

Expand Down Expand Up @@ -524,6 +542,7 @@ public void testInAppShowCampaignLabelFragmentUppercase() {
Mockito.when(screens.add("ShowedBatchInAppMessage")).thenReturn(screen);

TestEventPayload payload = new TestEventPayload(null,
null,
"https://batch.com/test#XtOr=" + xtor,
new Bundle());

Expand Down Expand Up @@ -553,6 +572,7 @@ public void testInAppShowCampaignLabelQueryUppercase() {
Mockito.when(screens.add("ShowedBatchInAppMessage")).thenReturn(screen);

TestEventPayload payload = new TestEventPayload(null,
null,
"https://batch.com/test?XTor=" + xtor,
new Bundle());

Expand Down Expand Up @@ -581,6 +601,7 @@ public void testInAppClickCampaignLabel() {
Mockito.when(screens.add("ClickedBatchInAppMessage")).thenReturn(screen);

TestEventPayload payload = new TestEventPayload(xtor,
null,
null,
new Bundle(), true);

Expand All @@ -597,6 +618,42 @@ public void testInAppClickCampaignLabel() {
Mockito.verify(screen).sendView();
}

@Test
public void testInAppWebViewClickCampaignLabel() {
String xtor = "EPR-[mylabel]-totot-titi";
String campaignExpected = "[mylabel]";
String webViewButtonIdExpected = "jesuisunbouton";

Publisher publisher = PowerMockito.mock(Publisher.class);
Mockito.when(publishers.add(campaignExpected)).thenReturn(publisher);

Screen screen = PowerMockito.mock(Screen.class);
CustomVars customVars = PowerMockito.mock(CustomVars.class);
Mockito.when(screens.add("WebViewClickedBatchInAppMessage")).thenReturn(screen);
Mockito.when(screen.CustomVars()).thenReturn(customVars);

TestEventPayload payload = new TestEventPayload(xtor,
webViewButtonIdExpected,
null,
new Bundle(), true);

atInternetDispatcher.dispatchEvent(Batch.EventDispatcher.Type.MESSAGING_WEBVIEW_CLICK, payload);

Mockito.verify(publishers).add(Mockito.eq(campaignExpected));
Mockito.verify(publisher).setAdvertiserId(Mockito.eq("[batch]"));
Mockito.verify(publisher).setFormat(Mockito.eq("[in-app]"));
Mockito.verify(publisher).setVariant(Mockito.eq("[" + webViewButtonIdExpected + "]"));
Mockito.verify(publisher).sendTouch();
Mockito.verify(publisher, Mockito.never()).sendImpression();

Mockito.verify(screens).add(Mockito.eq("WebViewClickedBatchInAppMessage"));
Mockito.verify(screen).Campaign(xtor);
Mockito.verify(screen).sendView();

Mockito.verify(screen).CustomVars();
Mockito.verify(customVars).add(1, webViewButtonIdExpected, CustomVar.CustomVarType.Screen);
}

@Test
public void testInAppClickNonPositive() {

Expand All @@ -607,6 +664,7 @@ public void testInAppClickNonPositive() {
Mockito.when(screens.add("ClickedBatchInAppMessage")).thenReturn(screen);

TestEventPayload payload = new TestEventPayload(null,
null,
null,
new Bundle(), false);

Expand All @@ -630,6 +688,7 @@ public void testInAppGlobalTap() {
Mockito.when(screens.add("ClickedBatchInAppMessage")).thenReturn(screen);

TestEventPayload payload = new TestEventPayload(null,
null,
null,
new Bundle(), true);

Expand All @@ -656,6 +715,7 @@ public void testInAppClose() {
Mockito.when(screens.add("ClosedBatchInAppMessage")).thenReturn(screen);

TestEventPayload payload = new TestEventPayload(null,
null,
null,
new Bundle());

Expand All @@ -668,6 +728,29 @@ public void testInAppClose() {
Mockito.verify(screen).sendView();
}

@Test
public void testInAppCloseError() {

Publisher publisher = PowerMockito.mock(Publisher.class);
Mockito.when(publishers.add("[batch-default-campaign]")).thenReturn(publisher);

Screen screen = PowerMockito.mock(Screen.class);
Mockito.when(screens.add("ClosedErrorBatchInAppMessage")).thenReturn(screen);

TestEventPayload payload = new TestEventPayload(null,
null,
null,
new Bundle());

atInternetDispatcher.dispatchEvent(Batch.EventDispatcher.Type.MESSAGING_CLOSE_ERROR, payload);
Mockito.verify(publisher, Mockito.never()).sendImpression();
Mockito.verify(publisher, Mockito.never()).sendTouch();

Mockito.verify(screens).add(Mockito.eq("ClosedErrorBatchInAppMessage"));
Mockito.verify(screen, Mockito.never()).Campaign(Mockito.anyString());
Mockito.verify(screen).sendView();
}

@Test
public void testInAppAutoClose() {

Expand All @@ -678,6 +761,7 @@ public void testInAppAutoClose() {
Mockito.when(screens.add("AutoClosedBatchInAppMessage")).thenReturn(screen);

TestEventPayload payload = new TestEventPayload(null,
null,
null,
new Bundle());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,33 @@ public class TestEventPayload implements Batch.EventDispatcher.Payload {

private String trackingId;
private String deeplink;
private String webViewAnalyticsID;
private Bundle customPayload;
private boolean isPositive;

TestEventPayload(String trackingId,
String deeplink,
Bundle customPayload)
{
this(trackingId, deeplink, customPayload, false);
this(trackingId, null, deeplink, customPayload, false);
}

TestEventPayload(String trackingId,
String webViewAnalyticsID,
String deeplink,
Bundle customPayload)
{
this(trackingId, webViewAnalyticsID, deeplink, customPayload, false);
}

TestEventPayload(String trackingId,
String webViewAnalyticsID,
String deeplink,
Bundle customPayload,
boolean isPositive)
{
this.trackingId = trackingId;
this.webViewAnalyticsID = webViewAnalyticsID;
this.deeplink = deeplink;
this.customPayload = customPayload;
this.isPositive = isPositive;
Expand Down Expand Up @@ -77,4 +88,9 @@ public BatchPushPayload getPushPayload()
{
return null;
}

@Nullable
public String getWebViewAnalyticsID() {
return webViewAnalyticsID;
}
}

0 comments on commit e8a6af9

Please sign in to comment.