Skip to content

Instantly share code, notes, and snippets.

@wullemsb
Created June 27, 2025 11:01
Show Gist options
  • Save wullemsb/a787cff33b577ade2859d64773f2864d to your computer and use it in GitHub Desktop.
Save wullemsb/a787cff33b577ade2859d64773f2864d to your computer and use it in GitHub Desktop.
public class ABTestTrackingService
{
private readonly ILogger<ABTestTrackingService> _logger;
private readonly IFeatureManager _featureManager;
public ABTestTrackingService(ILogger<ABTestTrackingService> logger, IFeatureManager featureManager)
{
_logger = logger;
_featureManager = featureManager;
}
public async Task TrackEvent(string eventName, string userId, Dictionary<string, object> properties = null)
{
var featureStates = new Dictionary<string, bool>();
// Capture current feature flag states for this user
foreach (var featureFlag in GetAllFeatureFlags())
{
featureStates[featureFlag] = await _featureManager.IsEnabledAsync(featureFlag);
}
var trackingData = new
{
EventName = eventName,
UserId = userId,
Timestamp = DateTime.UtcNow,
FeatureFlags = featureStates,
Properties = properties ?? new Dictionary<string, object>()
};
// Log the event (in production, you'd send this to your analytics system)
_logger.LogInformation("A/B Test Event: {TrackingData}", JsonSerializer.Serialize(trackingData));
// In a real application, you would send this data to your analytics platform
// Examples: Application Insights, Google Analytics, Mixpanel, etc.
}
private IEnumerable<string> GetAllFeatureFlags()
{
return new[]
{
FeatureFlags.NewCheckoutFlow,
FeatureFlags.EnhancedProductRecommendations,
FeatureFlags.BetaUserInterface
};
}
}
public class CheckoutController : Controller
{
private readonly ABTestTrackingService _trackingService;
public CheckoutController(ABTestTrackingService trackingService)
{
_trackingService = trackingService;
}
[HttpPost]
public async Task<IActionResult> CompletePurchase(PurchaseModel model)
{
// Process the purchase
var result = ProcessPurchase(model);
if (result.Success)
{
// Track the conversion event
await _trackingService.TrackEvent("Purchase Completed", model.UserId, new Dictionary<string, object>
{
["OrderValue"] = model.TotalAmount,
["ProductCount"] = model.Items.Count,
["CheckoutDuration"] = model.CheckoutDurationSeconds
});
}
return Json(result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment