Skip to content

Commit

Permalink
Add DeepLink entity referrer and url to atomic properties in ScreenVi…
Browse files Browse the repository at this point in the history
…ew events (close #553)
  • Loading branch information
matus-tomlein committed Nov 1, 2022
1 parent 2ed0c5e commit 9c6f07e
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 9 deletions.
Expand Up @@ -27,6 +27,8 @@

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -87,4 +89,58 @@ public void testWorkaroundForCampaignAttributionEnrichment() throws InterruptedE
assertEquals("url", url);
assertEquals("referrer", referrer);
}

@Test
public void testDeepLinkContextAndAtomicPropertiesAddedToScreenView() throws InterruptedException {
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

// Prepare DeepLinkReceived event
DeepLinkReceived event = new DeepLinkReceived("the_url")
.referrer("the_referrer");

// Prepare Screen View event
ScreenView screenView = new ScreenView("SV");

// Setup tracker
TrackerConfiguration trackerConfiguration = new TrackerConfiguration("appId")
.base64encoding(false)
.installAutotracking(false);
MockEventStore eventStore = new MockEventStore();
NetworkConfiguration networkConfiguration = new NetworkConfiguration("fake-url", HttpMethod.POST);
EmitterConfiguration emitterConfiguration = new EmitterConfiguration()
.eventStore(eventStore)
.threadPoolSize(10);
TrackerController trackerController = Snowplow.createTracker(context, "namespace", networkConfiguration, trackerConfiguration, emitterConfiguration);

// Track events
trackerController.track(event);
UUID screenViewEventId = trackerController.track(screenView);
for (int i=0; eventStore.getSize() < 1 && i < 10; i++) {
Thread.sleep(1000);
}
List<EmitterEvent> events = eventStore.getEmittableEvents(10);
eventStore.removeAllEvents();
assertEquals(2, events.size());

Map screenViewPayload = null;
for (EmitterEvent emitterEvent : events) {
if (Objects.equals(emitterEvent.payload.getMap().get("eid"), screenViewEventId.toString())) {
screenViewPayload = emitterEvent.payload.getMap();
break;
}
}
assertNotNull(screenViewPayload);

// Check the DeepLink context entity properties
String screenViewContext = (String) screenViewPayload.get(Parameters.CONTEXT);
assertNotNull(screenViewContext);
assertTrue(screenViewContext.contains("\"referrer\":\"the_referrer\""));
assertTrue(screenViewContext.contains("\"url\":\"the_url\""));

// Check url and referrer fields for atomic table
String url = (String)screenViewPayload.get(Parameters.PAGE_URL);
String referrer = (String)screenViewPayload.get(Parameters.PAGE_REFR);
assertEquals("the_url", url);
assertEquals("the_referrer", referrer);
}
}
Expand Up @@ -22,7 +22,6 @@
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;

import com.google.android.gms.common.internal.safeparcel.SafeParcelable;
import com.snowplowanalytics.snowplow.TestUtils;
import com.snowplowanalytics.snowplow.emitter.EventStore;
import com.snowplowanalytics.snowplow.event.SelfDescribing;
Expand Down
Expand Up @@ -44,5 +44,15 @@ public DeepLink referrer(@Nullable String referrer) {
setData(parameters);
return this;
}

@Nullable
public String getUrl() {
return (String) parameters.get(PARAM_DEEPLINK_URL);
}

@Nullable
public String getReferrer() {
return (String) parameters.get(PARAM_DEEPLINK_REFERRER);
}
}

Expand Up @@ -29,6 +29,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import com.snowplowanalytics.snowplow.entity.DeepLink;
import com.snowplowanalytics.snowplow.event.Background;
import com.snowplowanalytics.snowplow.event.DeepLinkReceived;
import com.snowplowanalytics.snowplow.event.Foreground;
Expand Down Expand Up @@ -663,6 +664,10 @@ private void transformEvent(@NonNull TrackerEvent event) {
addGlobalContextsToContexts(contexts, event);
addStateMachineEntitiesToContexts(contexts, event);
wrapContextsToPayload(payload, contexts);
if (!event.isPrimitive) {
// TODO: To remove when Atomic table refactoring is finished
workaroundForCampaignAttributionEnrichment(payload, event, contexts);
}
return payload;
}

Expand All @@ -689,8 +694,6 @@ private void addPrimitivePropertiesToPayload(@NonNull Payload payload, @NonNull
private void addSelfDescribingPropertiesToPayload(@NonNull Payload payload, @NonNull TrackerEvent event) {
payload.add(Parameters.EVENT, TrackerConstants.EVENT_UNSTRUCTURED);

workaroundForCampaignAttributionEnrichment(payload, event); // TODO: To remove when Atomic table refactoring is finished

SelfDescribingJson data = new SelfDescribingJson(event.schema, event.payload);
HashMap<String, Object> unstructuredEventPayload = new HashMap<>();
unstructuredEventPayload.put(Parameters.SCHEMA, TrackerConstants.SCHEMA_UNSTRUCT_EVENT);
Expand All @@ -701,19 +704,33 @@ private void addSelfDescribingPropertiesToPayload(@NonNull Payload payload, @Non
/*
This is needed because the campaign-attribution-enrichment (in the pipeline) is able to parse
the `url` and `referrer` only if they are part of a PageView event.
The PageView event is an atomic event but the DeepLinkReceived is a SelfDescribing event.
The PageView event is an atomic event but the DeepLinkReceived and ScreenView are SelfDescribing events.
For this reason we copy these two fields in the atomic fields in order to let the enrichment
to process correctly the fields even if the event is not a PageView and it's a SelfDescribing event.
This is a hack that should be removed once the atomic event table is dismissed and all the events
will be SelfDescribing.
*/
private void workaroundForCampaignAttributionEnrichment(@NonNull Payload payload, @NonNull TrackerEvent event) {
private void workaroundForCampaignAttributionEnrichment(@NonNull Payload payload, @NonNull TrackerEvent event, List<SelfDescribingJson> contexts) {
String url = null;
String referrer = null;

if (event.schema.equals(DeepLinkReceived.SCHEMA) && event.payload != null) {
String url = (String)event.payload.get(DeepLinkReceived.PARAM_URL);
String referrer = (String)event.payload.get(DeepLinkReceived.PARAM_REFERRER);
payload.add(Parameters.PAGE_URL, url);
payload.add(Parameters.PAGE_REFR, referrer);
url = (String)event.payload.get(DeepLinkReceived.PARAM_URL);
referrer = (String)event.payload.get(DeepLinkReceived.PARAM_REFERRER);
}
else if (event.schema.equals(TrackerConstants.SCHEMA_SCREEN_VIEW) && contexts != null) {
for (SelfDescribingJson entity : contexts) {
if (entity instanceof DeepLink) {
DeepLink deepLink = (DeepLink) entity;
url = deepLink.getUrl();
referrer = deepLink.getReferrer();
break;
}
}
}

if (url != null) { payload.add(Parameters.PAGE_URL, url); }
if (referrer != null) { payload.add(Parameters.PAGE_REFR, referrer); }
}

/*
Expand Down

0 comments on commit 9c6f07e

Please sign in to comment.