Skip to content

Commit 04ce75d

Browse files
feat(track): Introducing easier event tracking (#250)
1 parent db15b83 commit 04ce75d

File tree

12 files changed

+323
-778
lines changed

12 files changed

+323
-778
lines changed

core-api/src/main/java/com/optimizely/ab/Optimizely.java

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.optimizely.ab.bucketing.DecisionService;
2121
import com.optimizely.ab.bucketing.FeatureDecision;
2222
import com.optimizely.ab.bucketing.UserProfileService;
23-
import com.optimizely.ab.config.Attribute;
2423
import com.optimizely.ab.config.EventType;
2524
import com.optimizely.ab.config.Experiment;
2625
import com.optimizely.ab.config.FeatureFlag;
@@ -309,36 +308,14 @@ public void track(@Nonnull String eventName,
309308
logger.warn("Event tags is null when non-null was expected. Defaulting to an empty event tags map.");
310309
}
311310

312-
List<Experiment> experimentsForEvent = projectConfig.getExperimentsForEventKey(eventName);
313-
Map<Experiment, Variation> experimentVariationMap = new HashMap<Experiment, Variation>(experimentsForEvent.size());
314-
for (Experiment experiment : experimentsForEvent) {
315-
if (experiment.isRunning()) {
316-
Variation variation = decisionService.getVariation(experiment, userId, copiedAttributes);
317-
if (variation != null) {
318-
experimentVariationMap.put(experiment, variation);
319-
}
320-
} else {
321-
logger.info(
322-
"Not tracking event \"{}\" for experiment \"{}\" because experiment has status \"Launched\".",
323-
eventType.getKey(), experiment.getKey());
324-
}
325-
}
326-
327311
// create the conversion event request parameters, then dispatch
328312
LogEvent conversionEvent = eventFactory.createConversionEvent(
329-
projectConfig,
330-
experimentVariationMap,
331-
userId,
332-
eventType.getId(),
333-
eventType.getKey(),
334-
copiedAttributes,
335-
eventTags);
336-
337-
if (conversionEvent == null) {
338-
logger.info("There are no valid experiments for event \"{}\" to track.", eventName);
339-
logger.info("Not tracking event \"{}\" for user \"{}\".", eventName, userId);
340-
return;
341-
}
313+
projectConfig,
314+
userId,
315+
eventType.getId(),
316+
eventType.getKey(),
317+
copiedAttributes,
318+
eventTags);
342319

343320
logger.info("Tracking event \"{}\" for user \"{}\".", eventName, userId);
344321

core-api/src/main/java/com/optimizely/ab/event/internal/EventFactory.java

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -104,29 +104,12 @@ public LogEvent createImpressionEvent(@Nonnull ProjectConfig projectConfig,
104104
}
105105

106106
public LogEvent createConversionEvent(@Nonnull ProjectConfig projectConfig,
107-
@Nonnull Map<Experiment, Variation> experimentVariationMap,
108107
@Nonnull String userId,
109108
@Nonnull String eventId, // Why is this not used?
110109
@Nonnull String eventName,
111110
@Nonnull Map<String, ?> attributes,
112111
@Nonnull Map<String, ?> eventTags) {
113112

114-
if (experimentVariationMap.isEmpty()) {
115-
return null;
116-
}
117-
118-
ArrayList<Decision> decisions = new ArrayList<Decision>(experimentVariationMap.size());
119-
for (Map.Entry<Experiment, Variation> entry : experimentVariationMap.entrySet()) {
120-
Decision decision = new Decision.Builder()
121-
.setCampaignId(entry.getKey().getLayerId())
122-
.setExperimentId(entry.getKey().getId())
123-
.setVariationId(entry.getValue().getId())
124-
.setIsCampaignHoldback(false)
125-
.build();
126-
127-
decisions.add(decision);
128-
}
129-
130113
EventType eventType = projectConfig.getEventNameMapping().get(eventName);
131114

132115
Event conversionEvent = new Event.Builder()
@@ -141,9 +124,8 @@ public LogEvent createConversionEvent(@Nonnull ProjectConfig projectConfig,
141124
.build();
142125

143126
Snapshot snapshot = new Snapshot.Builder()
144-
.setDecisions(decisions)
145-
.setEvents(Collections.singletonList((conversionEvent)))
146-
.build();
127+
.setEvents(Collections.singletonList(conversionEvent))
128+
.build();
147129

148130
Visitor visitor = new Visitor.Builder()
149131
.setVisitorId(userId)

core-api/src/main/java/com/optimizely/ab/event/internal/payload/EventBatch.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public String getClientEngineValue() {
4444
@JsonProperty("account_id")
4545
String accountId;
4646
List<Visitor> visitors;
47+
@JsonProperty("enrich_decisions")
48+
Boolean enrichDecisions;
4749
@JsonProperty("anonymize_ip")
4850
Boolean anonymizeIp;
4951
@JsonProperty("client_name")
@@ -61,6 +63,7 @@ public EventBatch() {
6163
private EventBatch(String clientName, String clientVersion, String accountId, List<Visitor> visitors, Boolean anonymizeIp, String projectId, String revision) {
6264
this.accountId = accountId;
6365
this.visitors = visitors;
66+
this.enrichDecisions = true;
6467
this.anonymizeIp = anonymizeIp;
6568
this.clientName = clientName;
6669
this.clientVersion = clientVersion;
@@ -84,6 +87,12 @@ public void setVisitors(List<Visitor> visitors) {
8487
this.visitors = visitors;
8588
}
8689

90+
public Boolean getEnrichDecisions() { return enrichDecisions; }
91+
92+
public void setEnrichDecisions(Boolean enrichDecisions) {
93+
this.enrichDecisions = enrichDecisions;
94+
}
95+
8796
public Boolean getAnonymizeIp() {
8897
return anonymizeIp;
8998
}

core-api/src/main/java/com/optimizely/ab/event/internal/payload/Snapshot.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,11 @@ public boolean equals(Object o) {
6969
Snapshot snapshot = (Snapshot) o;
7070

7171
if (activationTimestamp != null ?
72-
!activationTimestamp.equals(snapshot.activationTimestamp) :
73-
snapshot.activationTimestamp != null) return false;
74-
if (!decisions.equals(snapshot.decisions)) return false;
72+
!activationTimestamp.equals(snapshot.activationTimestamp) :
73+
snapshot.activationTimestamp != null) return false;
74+
if (decisions != null ?
75+
!decisions.equals(snapshot.decisions) :
76+
snapshot.decisions != null) return false;
7577
return events.equals(snapshot.events);
7678
}
7779

core-api/src/main/java/com/optimizely/ab/event/internal/serializer/JsonSimpleSerializer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ private JSONObject serializeEventBatch(EventBatch eventBatch) {
4242
JSONObject jsonObject = new JSONObject();
4343

4444
jsonObject.put("account_id", eventBatch.getAccountId());
45+
jsonObject.put("enrich_decisions", eventBatch.getEnrichDecisions());
4546
jsonObject.put("visitors", serializeVisitors(eventBatch.getVisitors()));
4647
if (eventBatch.getAnonymizeIp() != null) jsonObject.put("anonymize_ip", eventBatch.getAnonymizeIp());
4748
if (eventBatch.getClientName() != null) jsonObject.put("client_name", eventBatch.getClientName());
@@ -50,7 +51,6 @@ private JSONObject serializeEventBatch(EventBatch eventBatch) {
5051
if (eventBatch.getRevision() != null) jsonObject.put("revision", eventBatch.getRevision());
5152

5253
return jsonObject;
53-
5454
}
5555

5656
private JSONArray serializeVisitors(List<Visitor> visitors) {
@@ -90,7 +90,7 @@ private JSONArray serializeSnapshots(List<Snapshot> snapshots) {
9090
private JSONObject serializeSnapshot(Snapshot snapshot) {
9191
JSONObject jsonObject = new JSONObject();
9292

93-
jsonObject.put("decisions", serializeDecisions(snapshot.getDecisions()));
93+
if (snapshot.getDecisions() != null) jsonObject.put("decisions", serializeDecisions(snapshot.getDecisions()));
9494
jsonObject.put("events", serializeEvents(snapshot.getEvents()));
9595

9696
return jsonObject;

0 commit comments

Comments
 (0)