Skip to content

Commit 03131c9

Browse files
msohailhussainmikeproeng37
authored andcommitted
fix(track): Send decisions for all experiments using an event when using track (#100)
1 parent 7ba5a66 commit 03131c9

File tree

5 files changed

+408
-96
lines changed

5 files changed

+408
-96
lines changed

OptimizelySDK.Tests/EventTests/EventBuilderTest.cs

Lines changed: 145 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class EventBuilderTest
3232
{
3333
private string TestUserId = string.Empty;
3434
private ProjectConfig Config;
35+
3536
private EventBuilder EventBuilder;
3637

3738
[TestFixtureSetUp]
@@ -83,7 +84,7 @@ public void TestCreateImpressionEventNoAttributes()
8384
}
8485
}
8586
},
86-
{"attributes", new object[]
87+
{"attributes", new object[]
8788
{
8889
new Dictionary<string, object>
8990
{
@@ -107,7 +108,7 @@ public void TestCreateImpressionEventNoAttributes()
107108
};
108109

109110
var expectedLogEvent = new LogEvent("https://logx.optimizely.com/v1/events",
110-
payloadParams,
111+
payloadParams,
111112
"POST",
112113
new Dictionary<string, string>
113114
{
@@ -191,10 +192,10 @@ public void TestCreateImpressionEventWithAttributes()
191192
{"revision", "15" },
192193
{"anonymize_ip", false}
193194
};
194-
195+
195196
var expectedLogEvent = new LogEvent("https://logx.optimizely.com/v1/events",
196197
payloadParams,
197-
"POST",
198+
"POST",
198199
new Dictionary<string, string>
199200
{
200201
{ "Content-Type", "application/json" }
@@ -255,7 +256,7 @@ public void TestCreateConversionEventNoAttributesNoValue()
255256
}
256257
},
257258
{"visitor_id", TestUserId },
258-
{"attributes", new object[]
259+
{"attributes", new object[]
259260
{
260261
new Dictionary<string, object>
261262
{
@@ -276,7 +277,7 @@ public void TestCreateConversionEventNoAttributesNoValue()
276277
{"revision", "15" },
277278
{"anonymize_ip", false}
278279
};
279-
280+
280281
var expectedEvent = new LogEvent(
281282
"https://logx.optimizely.com/v1/events",
282283
payloadParams,
@@ -302,7 +303,7 @@ public void TestCreateConversionEventWithAttributesNoValue()
302303
{
303304
var guid = Guid.NewGuid();
304305
var timeStamp = TestData.SecondsSince1970();
305-
306+
306307
var payloadParams = new Dictionary<string, object>
307308
{
308309
{"visitors", new object[]
@@ -597,7 +598,7 @@ public void TestCreateConversionEventWithAttributesWithValue()
597598

598599
Assert.IsTrue(TestData.CompareObjects(expectedEvent, logEvent));
599600
}
600-
601+
601602
[Test]
602603
public void TestCreateConversionEventNoAttributesWithInvalidValue()
603604
{
@@ -668,7 +669,7 @@ public void TestCreateConversionEventNoAttributesWithInvalidValue()
668669
{"revision", "15" },
669670
{"anonymize_ip", false}
670671
};
671-
672+
672673
var expectedEvent = new LogEvent(
673674
"https://logx.optimizely.com/v1/events",
674675
payloadParams,
@@ -989,7 +990,7 @@ public void TestCreateImpressionEventWithBucketingIDAttribute()
989990
{ "company", "Optimizely" },
990991
{ControlAttributes.BUCKETING_ID_ATTRIBUTE, "variation" }
991992
};
992-
993+
993994
var logEvent = EventBuilder.CreateImpressionEvent(Config, Config.GetExperimentFromKey("test_experiment"), "7722370027", TestUserId, userAttributes);
994995

995996
TestData.ChangeGUIDAndTimeStamp(logEvent.Params, timeStamp, guid);
@@ -1362,5 +1363,139 @@ public void TestCreateConversionEventWhenBotFilteringIsNotProvidedInDatafile()
13621363

13631364
Assert.IsTrue(TestData.CompareObjects(expectedEvent, logEvent));
13641365
}
1366+
1367+
[Test]
1368+
public void TestCreateConversionEventWhenEventUsedInMultipleExp()
1369+
{
1370+
var guid = Guid.NewGuid();
1371+
var timeStamp = TestData.SecondsSince1970();
1372+
1373+
var eventInMultiExperimentConfig = ProjectConfig.Create(TestData.SimpleABExperimentsDatafile, new NoOpLogger(), new ErrorHandler.NoOpErrorHandler());
1374+
1375+
var experimentIdVariationMap = new Dictionary<string, Variation>
1376+
{
1377+
{
1378+
"111127", new Variation{Id="111129", Key="variation"}
1379+
},
1380+
{
1381+
"111130", new Variation{Id="111131", Key="variation"}
1382+
}
1383+
};
1384+
1385+
var logEvent = EventBuilder.CreateConversionEvent(eventInMultiExperimentConfig, "event_with_multiple_running_experiments", experimentIdVariationMap, "test_user",
1386+
new UserAttributes {
1387+
{"test_attribute", "test_value"}
1388+
},
1389+
new EventTags {
1390+
{"revenue", 4200},
1391+
{"value", 1.234},
1392+
{"non-revenue", "abc"}
1393+
});
1394+
1395+
var payloadParams = new Dictionary<string, object>
1396+
{
1397+
{"client_version", Optimizely.SDK_VERSION},
1398+
{"project_id", "111001"},
1399+
//{"visitor_id", "test_user"},
1400+
{"account_id", "12001"},
1401+
{"client_name", "csharp-sdk"},
1402+
{"anonymize_ip", false},
1403+
{"revision", eventInMultiExperimentConfig.Revision},
1404+
{"visitors", new object[]
1405+
{
1406+
//visitors[0]
1407+
new Dictionary<string, object>
1408+
{
1409+
//visitors[0].attributes
1410+
{
1411+
"attributes", new object[]
1412+
{
1413+
new Dictionary<string, string>
1414+
{
1415+
{"entity_id", "111094"},
1416+
{"type", "custom"},
1417+
{"value", "test_value"},
1418+
{"key", "test_attribute"}
1419+
}
1420+
}
1421+
},
1422+
//visitors[0].visitor_id
1423+
{"visitor_id", "test_user"},
1424+
//visitors[0].snapshots
1425+
{"snapshots", new object[]
1426+
{
1427+
//snapshots[0]
1428+
new Dictionary<string, object>
1429+
{
1430+
//snapshots[0].decisions
1431+
{"decisions", new object[]
1432+
{
1433+
//decisions[0]
1434+
new Dictionary<string, object>
1435+
{
1436+
{"variation_id", "111129"},
1437+
{"experiment_id", "111127"},
1438+
{"campaign_id", "111182"}
1439+
1440+
},
1441+
//decisions[1]
1442+
new Dictionary<string,object>
1443+
{
1444+
{"experiment_id", "111130"},
1445+
{"variation_id", "111131"},
1446+
{"campaign_id", "111182"}
1447+
}
1448+
}
1449+
},
1450+
//snapshots[0].events
1451+
{
1452+
"events", new object[]
1453+
{
1454+
new Dictionary<string, object>
1455+
{
1456+
{"uuid", guid},
1457+
{"timestamp", timeStamp},
1458+
{"revenue", 4200},
1459+
{"value", 1.234},
1460+
{"key", "event_with_multiple_running_experiments"},
1461+
{"entity_id", "111095"},
1462+
{
1463+
"tags", new Dictionary<string, object>
1464+
{
1465+
{"non-revenue", "abc"},
1466+
{"revenue", 4200},
1467+
{"value", 1.234},
1468+
}
1469+
}
1470+
1471+
}
1472+
}
1473+
}
1474+
1475+
}
1476+
1477+
}
1478+
}
1479+
1480+
}
1481+
}
1482+
1483+
}
1484+
};
1485+
1486+
1487+
var expectedLogEvent = new LogEvent(
1488+
"https://logx.optimizely.com/v1/events",
1489+
payloadParams,
1490+
"POST",
1491+
new Dictionary<string, string>
1492+
{
1493+
{ "Content-Type", "application/json"}
1494+
});
1495+
1496+
TestData.ChangeGUIDAndTimeStamp(logEvent.Params, timeStamp, guid);
1497+
1498+
Assert.IsTrue(TestData.CompareObjects(expectedLogEvent, logEvent));
1499+
}
13651500
}
13661501
}

OptimizelySDK.Tests/OptimizelySDK.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
</ItemGroup>
9595
<ItemGroup>
9696
<EmbeddedResource Include="TestData.json" />
97+
<EmbeddedResource Include="simple_ab_experiments.json" />
9798
</ItemGroup>
9899
<ItemGroup>
99100
<ProjectReference Include="..\OptimizelySDK\OptimizelySDK.csproj">

OptimizelySDK.Tests/TestData.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,23 @@ namespace OptimizelySDK.Tests
2424
public class TestData
2525
{
2626
private static string cachedDataFile = null;
27-
27+
private static string simpleABExperimentsDatafile = null;
28+
2829
public static string Datafile
2930
{
3031
get
3132
{
3233
return cachedDataFile ?? (cachedDataFile = LoadJsonData());
3334
}
3435
}
36+
37+
public static string SimpleABExperimentsDatafile
38+
{
39+
get
40+
{
41+
return simpleABExperimentsDatafile ?? (simpleABExperimentsDatafile = LoadJsonData("simple_ab_experiments.json"));
42+
}
43+
}
3544

3645
private static string LoadJsonData(string fileName = "TestData.json")
3746
{

0 commit comments

Comments
 (0)