|
| 1 | +package org.prebid.server.analytics.reporter.liveintent; |
| 2 | + |
| 3 | +import com.iab.openrtb.request.BidRequest; |
| 4 | +import com.iab.openrtb.response.Bid; |
| 5 | +import com.iab.openrtb.response.BidResponse; |
| 6 | +import com.iab.openrtb.response.SeatBid; |
| 7 | +import io.vertx.core.Future; |
| 8 | +import org.apache.commons.collections4.CollectionUtils; |
| 9 | +import org.apache.http.client.utils.URIBuilder; |
| 10 | +import org.prebid.server.analytics.AnalyticsReporter; |
| 11 | +import org.prebid.server.analytics.model.AuctionEvent; |
| 12 | +import org.prebid.server.analytics.model.NotificationEvent; |
| 13 | +import org.prebid.server.analytics.reporter.liveintent.model.LiveIntentAnalyticsProperties; |
| 14 | +import org.prebid.server.analytics.reporter.liveintent.model.PbsjBid; |
| 15 | +import org.prebid.server.auction.model.AuctionContext; |
| 16 | +import org.prebid.server.exception.PreBidException; |
| 17 | +import org.prebid.server.hooks.execution.model.ExecutionStatus; |
| 18 | +import org.prebid.server.hooks.execution.model.GroupExecutionOutcome; |
| 19 | +import org.prebid.server.hooks.execution.model.HookExecutionContext; |
| 20 | +import org.prebid.server.hooks.execution.model.HookExecutionOutcome; |
| 21 | +import org.prebid.server.hooks.execution.model.Stage; |
| 22 | +import org.prebid.server.hooks.execution.model.StageExecutionOutcome; |
| 23 | +import org.prebid.server.hooks.v1.analytics.Activity; |
| 24 | +import org.prebid.server.hooks.v1.analytics.Tags; |
| 25 | +import org.prebid.server.json.JacksonMapper; |
| 26 | +import org.prebid.server.log.Logger; |
| 27 | +import org.prebid.server.log.LoggerFactory; |
| 28 | +import org.prebid.server.proto.openrtb.ext.request.ExtRequest; |
| 29 | +import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebid; |
| 30 | +import org.prebid.server.vertx.httpclient.HttpClient; |
| 31 | + |
| 32 | +import java.net.URISyntaxException; |
| 33 | +import java.util.Collection; |
| 34 | +import java.util.List; |
| 35 | +import java.util.Objects; |
| 36 | +import java.util.Optional; |
| 37 | + |
| 38 | +public class LiveIntentAnalyticsReporter implements AnalyticsReporter { |
| 39 | + |
| 40 | + private static final Logger logger = LoggerFactory.getLogger(LiveIntentAnalyticsReporter.class); |
| 41 | + |
| 42 | + private static final String LIVEINTENT_HOOK_ID = "liveintent-omni-channel-identity-enrichment-hook"; |
| 43 | + |
| 44 | + private final HttpClient httpClient; |
| 45 | + private final LiveIntentAnalyticsProperties properties; |
| 46 | + private final JacksonMapper jacksonMapper; |
| 47 | + |
| 48 | + public LiveIntentAnalyticsReporter( |
| 49 | + LiveIntentAnalyticsProperties properties, |
| 50 | + HttpClient httpClient, |
| 51 | + JacksonMapper jacksonMapper) { |
| 52 | + |
| 53 | + this.httpClient = Objects.requireNonNull(httpClient); |
| 54 | + this.properties = Objects.requireNonNull(properties); |
| 55 | + this.jacksonMapper = Objects.requireNonNull(jacksonMapper); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public <T> Future<Void> processEvent(T event) { |
| 60 | + if (event instanceof AuctionEvent auctionEvent) { |
| 61 | + return processAuctionEvent(auctionEvent.getAuctionContext()); |
| 62 | + } else if (event instanceof NotificationEvent notificationEvent) { |
| 63 | + return processNotificationEvent(notificationEvent); |
| 64 | + } |
| 65 | + |
| 66 | + return Future.succeededFuture(); |
| 67 | + } |
| 68 | + |
| 69 | + private Future<Void> processAuctionEvent(AuctionContext auctionContext) { |
| 70 | + if (auctionContext.getBidRequest() == null) { |
| 71 | + return Future.failedFuture(new PreBidException("Bid request should not be empty")); |
| 72 | + } |
| 73 | + |
| 74 | + if (auctionContext.getBidResponse() == null) { |
| 75 | + return Future.succeededFuture(); |
| 76 | + } |
| 77 | + |
| 78 | + final BidRequest bidRequest = auctionContext.getBidRequest(); |
| 79 | + final BidResponse bidResponse = auctionContext.getBidResponse(); |
| 80 | + |
| 81 | + final List<Activity> activities = getActivities(auctionContext); |
| 82 | + final boolean isEnriched = isEnriched(activities); |
| 83 | + final Float treatmentRate = getTreatmentRate(activities); |
| 84 | + final Long timestamp = Optional.ofNullable(bidRequest.getExt()) |
| 85 | + .map(ExtRequest::getPrebid) |
| 86 | + .map(ExtRequestPrebid::getAuctiontimestamp) |
| 87 | + .orElse(0L); |
| 88 | + |
| 89 | + final List<PbsjBid> pbsjBids = CollectionUtils.emptyIfNull(bidResponse.getSeatbid()).stream() |
| 90 | + .map(SeatBid::getBid) |
| 91 | + .flatMap(Collection::stream) |
| 92 | + .map(bid -> buildPbsjBid(bidRequest, bidResponse, bid, isEnriched, treatmentRate, timestamp)) |
| 93 | + .filter(Optional::isPresent) |
| 94 | + .map(Optional::get) |
| 95 | + .toList(); |
| 96 | + |
| 97 | + try { |
| 98 | + return httpClient.post( |
| 99 | + new URIBuilder(properties.getAnalyticsEndpoint()) |
| 100 | + .setPath("/analytic-events/pbsj-bids") |
| 101 | + .build() |
| 102 | + .toString(), |
| 103 | + jacksonMapper.encodeToString(pbsjBids), |
| 104 | + properties.getTimeoutMs()) |
| 105 | + .mapEmpty(); |
| 106 | + } catch (Exception e) { |
| 107 | + logger.error("Error processing event: {}", e.getMessage()); |
| 108 | + return Future.failedFuture(e); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private List<Activity> getActivities(AuctionContext auctionContext) { |
| 113 | + return Optional.ofNullable(auctionContext) |
| 114 | + .map(AuctionContext::getHookExecutionContext) |
| 115 | + .map(HookExecutionContext::getStageOutcomes) |
| 116 | + .map(stages -> stages.get(Stage.processed_auction_request)) |
| 117 | + .stream() |
| 118 | + .flatMap(Collection::stream) |
| 119 | + .filter(stageExecutionOutcome -> "auction-request".equals(stageExecutionOutcome.getEntity())) |
| 120 | + .map(StageExecutionOutcome::getGroups) |
| 121 | + .flatMap(Collection::stream) |
| 122 | + .map(GroupExecutionOutcome::getHooks) |
| 123 | + .flatMap(Collection::stream) |
| 124 | + .filter(hook -> LIVEINTENT_HOOK_ID.equals(hook.getHookId().getModuleCode()) |
| 125 | + && hook.getStatus() == ExecutionStatus.success) |
| 126 | + .map(HookExecutionOutcome::getAnalyticsTags) |
| 127 | + .filter(Objects::nonNull) |
| 128 | + .map(Tags::activities) |
| 129 | + .filter(Objects::nonNull) |
| 130 | + .flatMap(Collection::stream) |
| 131 | + .filter(Objects::nonNull) |
| 132 | + .toList(); |
| 133 | + } |
| 134 | + |
| 135 | + private boolean isEnriched(List<Activity> activity) { |
| 136 | + return activity.stream().anyMatch(a -> "liveintent-enriched".equals(a.name())); |
| 137 | + } |
| 138 | + |
| 139 | + private Float getTreatmentRate(List<Activity> activity) { |
| 140 | + return activity.stream() |
| 141 | + .flatMap(a -> a.results().stream()) |
| 142 | + .filter(a -> a.values().has("treatmentRate")) |
| 143 | + .findFirst() |
| 144 | + .map(a -> a.values().get("treatmentRate").floatValue()) |
| 145 | + .orElse(null); |
| 146 | + } |
| 147 | + |
| 148 | + private Optional<PbsjBid> buildPbsjBid( |
| 149 | + BidRequest bidRequest, |
| 150 | + BidResponse bidResponse, |
| 151 | + Bid bid, |
| 152 | + boolean isEnriched, |
| 153 | + Float treatmentRate, |
| 154 | + Long timestamp) { |
| 155 | + |
| 156 | + return bidRequest.getImp().stream() |
| 157 | + .filter(impItem -> impItem.getId().equals(bid.getImpid())) |
| 158 | + .map(imp -> PbsjBid.builder() |
| 159 | + .bidId(bid.getId()) |
| 160 | + .price(bid.getPrice()) |
| 161 | + .adUnitId(imp.getTagid()) |
| 162 | + .enriched(isEnriched) |
| 163 | + .currency(bidResponse.getCur()) |
| 164 | + .treatmentRate(treatmentRate) |
| 165 | + .timestamp(timestamp) |
| 166 | + .partnerId(properties.getPartnerId()) |
| 167 | + .build()) |
| 168 | + .findFirst(); |
| 169 | + } |
| 170 | + |
| 171 | + private Future<Void> processNotificationEvent(NotificationEvent notificationEvent) { |
| 172 | + try { |
| 173 | + final String url = new URIBuilder(properties.getAnalyticsEndpoint()) |
| 174 | + .setPath("/analytic-events/pbsj-winning-bid") |
| 175 | + .setParameter("b", notificationEvent.getBidder()) |
| 176 | + .setParameter("bidId", notificationEvent.getBidId()) |
| 177 | + .build() |
| 178 | + .toString(); |
| 179 | + return httpClient.get(url, properties.getTimeoutMs()).mapEmpty(); |
| 180 | + } catch (URISyntaxException e) { |
| 181 | + logger.error("Error composing url for notification event: {}", e.getMessage()); |
| 182 | + return Future.failedFuture(e); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public int vendorId() { |
| 188 | + return 0; |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + public String name() { |
| 193 | + return "liveintentAnalytics"; |
| 194 | + } |
| 195 | +} |
0 commit comments