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

Win event URL #701

Merged
merged 5 commits into from
Oct 12, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -200,20 +200,8 @@ public void fetchDemand(OnFetchDemandResult listener) {
allowNullableAdObject = true;

fetchDemand(null, resultCode -> {
BidInfo bidInfo;
if (bidResponse != null) {
bidInfo = new BidInfo(resultCode, bidResponse.getTargeting());
if (resultCode == ResultCode.SUCCESS) {
boolean isNative = configuration.getNativeConfiguration() != null;
if (isNative) {
String cacheId = CacheManager.save(bidResponse.getWinningBidJson());
Util.saveCacheId(cacheId, adObject);
bidInfo.setNativeResult(cacheId, bidResponse.getExpirationTimeSeconds());
}
}
} else {
bidInfo = new BidInfo(resultCode, null);
}
BidInfo bidInfo = BidInfo.create(resultCode, bidResponse, configuration);
Util.saveCacheId(bidInfo.getNativeCacheId(), adObject);
listener.onComplete(bidInfo);
});
}
Expand Down Expand Up @@ -406,6 +394,11 @@ public void setPbAdSlot(String pbAdSlot) {
configuration.setPbAdSlot(pbAdSlot);
}

@Nullable
public String getGpid() {
return configuration.getGpid();
}

public void setGpid(@Nullable String gpid) {
configuration.setGpid(gpid);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,39 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.prebid.mobile.CacheManager;
import org.prebid.mobile.ResultCode;
import org.prebid.mobile.configuration.AdUnitConfiguration;
import org.prebid.mobile.rendering.bidding.data.bid.Bid;
import org.prebid.mobile.rendering.bidding.data.bid.BidResponse;

import java.util.Map;

public class BidInfo {

@NonNull
private ResultCode resultCode;
private final ResultCode resultCode;
@Nullable
private Map<String, String> targetingKeywords;
@Nullable
private Map<String, String> events;
@Nullable
private String nativeCacheId;
@Nullable
private Integer exp;

public BidInfo(
@NonNull ResultCode resultCode,
@Nullable Map<String, String> targetingKeywords
) {
this.resultCode = resultCode;
this.targetingKeywords = targetingKeywords;
}
/**
* Key for {@link #getEvents()} map to get win event.
*/
public static final String EVENT_WIN = "ext.prebid.events.win";
/**
* Key for {@link #getEvents()} map to get impression event.
*/
public static final String EVENT_IMP = "ext.prebid.events.imp";

public void setNativeResult(
String nativeCacheId,
Integer expirationTimeSeconds
) {
this.nativeCacheId = nativeCacheId;
this.exp = expirationTimeSeconds;

private BidInfo(@NonNull ResultCode resultCode) {
this.resultCode = resultCode;
}

@NonNull
Expand All @@ -54,4 +58,38 @@ public Integer getExp() {
return exp;
}

@Nullable
public Map<String, String> getEvents() {
return events;
}


@NonNull
public static BidInfo create(
@NonNull ResultCode resultCode,
@Nullable BidResponse bidResponse,
@Nullable AdUnitConfiguration configuration
) {
BidInfo bidInfo = new BidInfo(resultCode);
if (bidResponse == null) {
return bidInfo;
}

bidInfo.targetingKeywords = bidResponse.getTargeting();

bidInfo.exp = bidResponse.getExpirationTimeSeconds();

Bid winningBid = bidResponse.getWinningBid();
if (winningBid != null) {
bidInfo.events = winningBid.getEvents();
}

boolean isNative = configuration != null && configuration.getNativeConfiguration() != null;
if (isNative && bidInfo.resultCode == ResultCode.SUCCESS) {
bidInfo.nativeCacheId = CacheManager.save(bidResponse.getWinningBidJson());
}

return bidInfo;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.prebid.mobile.api.original;

import android.annotation.SuppressLint;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

Expand All @@ -16,6 +18,7 @@
import org.prebid.mobile.VideoParameters;
import org.prebid.mobile.api.data.AdFormat;
import org.prebid.mobile.api.exceptions.AdException;
import org.prebid.mobile.configuration.AdUnitConfiguration;
import org.prebid.mobile.configuration.NativeAdUnitConfiguration;
import org.prebid.mobile.rendering.bidding.data.bid.BidResponse;
import org.prebid.mobile.rendering.bidding.listeners.BidRequesterListener;
Expand Down Expand Up @@ -135,4 +138,10 @@ public BidResponse getBidResponse() {
return bidResponse;
}

@SuppressLint("VisibleForTests")
@Override
public AdUnitConfiguration getConfiguration() {
return super.getConfiguration();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.prebid.mobile.CacheManager;
import org.prebid.mobile.OnCompleteListener;
import org.prebid.mobile.OnCompleteListener2;
import org.prebid.mobile.ResultCode;
import org.prebid.mobile.Util;
import org.prebid.mobile.api.data.BidInfo;
import org.prebid.mobile.rendering.bidding.data.bid.BidResponse;

import java.util.Map;

Expand All @@ -19,23 +17,19 @@
class OnCompleteListenerImpl implements OnCompleteListener, OnCompleteListener2 {

@Nullable
private Object adObject;
private final Object adObject;
@NonNull
private PrebidRequest request;
private final MultiformatAdUnitFacade adUnit;
@NonNull
private MultiformatAdUnitFacade adUnit;
@NonNull
private OnFetchDemandResult listener;
private final OnFetchDemandResult listener;

OnCompleteListenerImpl(
@NonNull MultiformatAdUnitFacade adUnit,
@NonNull PrebidRequest request,
@Nullable Object adObject,
@NonNull OnFetchDemandResult listener
) {
this.adObject = adObject;
this.adUnit = adUnit;
this.request = request;
this.listener = listener;
}

Expand All @@ -51,31 +45,9 @@ public void onComplete(ResultCode resultCode, @Nullable Map<String, String> unmo


private void notifyListener(ResultCode resultCode) {
BidResponse bidResponse = adUnit.getBidResponse();

if (bidResponse == null) {
listener.onComplete(new BidInfo(resultCode, null));
return;
}

BidInfo bidInfo = new BidInfo(resultCode, bidResponse.getTargeting());
saveCacheForNativeIfNeeded(bidResponse, bidInfo, resultCode);
BidInfo bidInfo = BidInfo.create(resultCode, adUnit.getBidResponse(), adUnit.getConfiguration());
Util.saveCacheId(bidInfo.getNativeCacheId(), adObject);
listener.onComplete(bidInfo);
}

private void saveCacheForNativeIfNeeded(
BidResponse bidResponse,
BidInfo bidInfo,
ResultCode resultCode
) {
if (resultCode == ResultCode.SUCCESS) {
boolean isNative = request.getNativeParameters() != null;
if (isNative) {
String cacheId = CacheManager.save(bidResponse.getWinningBidJson());
Util.saveCacheId(cacheId, adObject);
bidInfo.setNativeResult(cacheId, bidResponse.getExpirationTimeSeconds());
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private void baseFetchDemand(
}

if (request == null || requestDoesNotHaveAnyConfiguration(request)) {
userListener.onComplete(new BidInfo(ResultCode.INVALID_PREBID_REQUEST_OBJECT, null));
userListener.onComplete(BidInfo.create(ResultCode.INVALID_PREBID_REQUEST_OBJECT, null, null));
return;
}

Expand All @@ -85,7 +85,7 @@ private void baseFetchDemand(

adUnit = new MultiformatAdUnitFacade(configId, request);

OnCompleteListenerImpl innerListener = new OnCompleteListenerImpl(adUnit, request, adObject, userListener);
OnCompleteListenerImpl innerListener = new OnCompleteListenerImpl(adUnit, request, userListener);
if (adObject != null) {
adUnit.fetchDemand(adObject, innerListener);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@


import android.util.Base64;

import androidx.annotation.Nullable;

import org.json.JSONArray;
import org.json.JSONObject;
import org.prebid.mobile.api.data.BidInfo;
import org.prebid.mobile.rendering.models.internal.MacrosModel;
import org.prebid.mobile.rendering.models.openrtb.bidRequests.MobileSdkPassThrough;
import org.prebid.mobile.rendering.utils.helpers.MacrosResolutionHelper;
Expand Down Expand Up @@ -126,6 +130,9 @@ public class Bid {
// wait between the auction and the actual impression
private int exp;

@Nullable
private Map<String, String> events;

private MobileSdkPassThrough mobileSdkPassThrough;

protected Bid() {
Expand Down Expand Up @@ -250,6 +257,11 @@ public MobileSdkPassThrough getMobileSdkPassThrough() {
return mobileSdkPassThrough;
}

@Nullable
public Map<String, String> getEvents() {
return events;
}

public static Bid fromJSONObject(JSONObject jsonObject) {
Bid bid = new Bid();
if (jsonObject == null) {
Expand Down Expand Up @@ -286,7 +298,9 @@ public static Bid fromJSONObject(JSONObject jsonObject) {

JSONObject ext = jsonObject.optJSONObject("ext");
if (ext != null) {
bid.prebid = Prebid.fromJSONObject(ext.optJSONObject("prebid"));
Prebid prebidObject = Prebid.fromJSONObject(ext.optJSONObject("prebid"));
setEvents(bid, prebidObject);
bid.prebid = prebidObject;
bid.mobileSdkPassThrough = MobileSdkPassThrough.create(ext);
}

Expand Down Expand Up @@ -341,4 +355,21 @@ private static void substituteMacros(Bid bid) {
bid.adm = MacrosResolutionHelper.resolveAuctionMacros(bid.adm, macrosModelMap);
bid.nurl = MacrosResolutionHelper.resolveAuctionMacros(bid.nurl, macrosModelMap);
}


private static void setEvents(Bid bid, Prebid prebidObject) {
HashMap<String, String> events = new HashMap<>();
String winUrl = prebidObject.getWinEventUrl();
if (winUrl != null) {
events.put(BidInfo.EVENT_WIN, winUrl);
}
String impUrl = prebidObject.getImpEventUrl();
if (impUrl != null) {
events.put(BidInfo.EVENT_IMP, impUrl);
}
if (!events.isEmpty()) {
bid.events = events;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.prebid.mobile.NativeParameters;
import org.prebid.mobile.ResultCode;
import org.prebid.mobile.api.data.BidInfo;
import org.prebid.mobile.rendering.bidding.data.bid.Bid;
import org.prebid.mobile.rendering.bidding.data.bid.BidResponse;

import java.util.HashMap;
Expand Down Expand Up @@ -48,7 +49,6 @@ public void destroy() throws Exception {
public void emptyBidResponse() {
OnCompleteListenerImpl subject = new OnCompleteListenerImpl(
mockAdUnit,
mockPrebidRequest,
null,
mockListener
);
Expand All @@ -66,13 +66,13 @@ public void emptyBidResponse() {
assertNull(bidInfo.getNativeCacheId());
assertNull(bidInfo.getExp());
assertNull(bidInfo.getTargetingKeywords());
assertNull(bidInfo.getEvents());
}

@Test
public void fullBidResponse() {
OnCompleteListenerImpl subject = new OnCompleteListenerImpl(
mockAdUnit,
mockPrebidRequest,
null,
mockListener
);
Expand All @@ -81,6 +81,7 @@ public void fullBidResponse() {
keywords.put("key1", "value1");
keywords.put("key2", "value2");
when(mockBidResponse.getTargeting()).thenReturn(keywords);
when(mockBidResponse.getExpirationTimeSeconds()).thenReturn(null);
when(mockAdUnit.getBidResponse()).thenReturn(mockBidResponse);

subject.onComplete(ResultCode.SUCCESS);
Expand All @@ -94,24 +95,31 @@ public void fullBidResponse() {
assertEquals(keywords, bidInfo.getTargetingKeywords());
assertNull(bidInfo.getNativeCacheId());
assertNull(bidInfo.getExp());
assertNull(bidInfo.getEvents());
}

@Test
public void fullBidResponseWithNative() {
OnCompleteListenerImpl subject = new OnCompleteListenerImpl(
mockAdUnit,
mockPrebidRequest,
null,
mockListener
);

HashMap<String, String> keywords = new HashMap<>();
keywords.put("key1", "value1");
keywords.put("key2", "value2");
HashMap<String, String> events = new HashMap<>();
keywords.put("event1", "url1");
keywords.put("event2", "url2");
Bid mockBid = mock(Bid.class);
when(mockBid.getEvents()).thenReturn(events);

when(mockPrebidRequest.getNativeParameters()).thenReturn(mock(NativeParameters.class));
when(mockBidResponse.getTargeting()).thenReturn(keywords);
when(mockAdUnit.getBidResponse()).thenReturn(mockBidResponse);
when(mockBidResponse.getExpirationTimeSeconds()).thenReturn(300);
when(mockBidResponse.getWinningBid()).thenReturn(mockBid);

subject.onComplete(ResultCode.SUCCESS);

Expand All @@ -122,6 +130,7 @@ public void fullBidResponseWithNative() {
assertNotNull(bidInfo);
assertEquals(ResultCode.SUCCESS, bidInfo.getResultCode());
assertEquals(keywords, bidInfo.getTargetingKeywords());
assertEquals(events, bidInfo.getEvents());
assertEquals(Integer.valueOf(300), bidInfo.getExp());
}

Expand Down
Loading