-
Notifications
You must be signed in to change notification settings - Fork 20
bot filtering #79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bot filtering #79
Changes from 1 commit
3bd25d5
777f86a
d660fcd
66396d5
b746642
0540ccc
ace69dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -634,5 +634,6 @@ | |
"key": "purchase" | ||
}], | ||
"revision": "15", | ||
"anonymizeIP": "false" | ||
"anonymizeIP": "false", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will always be a boolean value |
||
"botFiltering": "true" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will always be a boolean value. |
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit. Add new line at end of file. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,9 @@ public class EventBuilder | |
|
||
private const string ACTIVATE_EVENT_KEY = "campaign_activated"; | ||
|
||
public const string RESERVED_ATTRIBUTE_KEY_BUCKETING_ID_EVENT_PARAM_KEY = "optimizely_bucketing_id"; | ||
public const string BOT_FILTERING_ATTRIBUTE = "$opt_bot_filtering"; | ||
|
||
public const string USER_AGENT_ATTRIBUTE = "$opt_user_agent"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit. Like in other SDKs, lets consolidate all of the "special" attributes i.e. bucketing ID, bot filtering and user agent attributes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
||
private static readonly Dictionary<string, string> HTTP_HEADERS = new Dictionary<string, string> | ||
{ | ||
|
@@ -103,37 +105,31 @@ private Dictionary<string, object> GetCommonParams(ProjectConfig config, string | |
|
||
foreach (var userAttribute in userAttributes.Where(a => !string.IsNullOrEmpty(a.Key))) | ||
{ | ||
if (string.Equals(userAttribute.Key, DecisionService.RESERVED_ATTRIBUTE_KEY_BUCKETING_ID)) | ||
var attributeId = config.GetAttributeId(userAttribute.Key); | ||
if (!string.IsNullOrEmpty(attributeId)) | ||
{ | ||
var userFeature = new Dictionary<string, object> | ||
userFeatures.Add(new Dictionary<string, object> | ||
{ | ||
{ "entity_id", DecisionService.RESERVED_ATTRIBUTE_KEY_BUCKETING_ID }, | ||
{ "key", RESERVED_ATTRIBUTE_KEY_BUCKETING_ID_EVENT_PARAM_KEY }, | ||
{ "entity_id", attributeId }, | ||
{ "key", userAttribute.Key }, | ||
{ "type", CUSTOM_ATTRIBUTE_FEATURE_TYPE }, | ||
{ "value", userAttribute.Value} | ||
}; | ||
userFeatures.Add(userFeature); | ||
} | ||
else | ||
{ | ||
var attributeEntity = config.GetAttribute(userAttribute.Key); | ||
if (attributeEntity != null && attributeEntity.Key != null) | ||
{ | ||
var userFeature = new Dictionary<string, object> | ||
{ | ||
{ "entity_id", attributeEntity.Id }, | ||
{ "key", attributeEntity.Key }, | ||
{ "type", CUSTOM_ATTRIBUTE_FEATURE_TYPE }, | ||
{ "value", userAttribute.Value} | ||
}; | ||
userFeatures.Add(userFeature); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
|
||
if (config.BotFiltering.HasValue) | ||
{ | ||
userFeatures.Add(new Dictionary<string, object> | ||
{ | ||
{ "entity_id", BOT_FILTERING_ATTRIBUTE }, | ||
{ "key", BOT_FILTERING_ATTRIBUTE }, | ||
{ "type", CUSTOM_ATTRIBUTE_FEATURE_TYPE }, | ||
{ "value", config.BotFiltering} | ||
}); | ||
} | ||
|
||
visitor["attributes"] = userFeatures; | ||
|
||
return comonParams; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ public enum LogLevel | |
DEBUG, | ||
INFO, | ||
ERROR, | ||
WARN, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WARN should go above ERROR IMO There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, update year |
||
} | ||
|
||
public interface ILogger | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,5 +1,5 @@ | ||||||
/* | ||||||
* Copyright 2017, Optimizely | ||||||
* Copyright 2017-2018, Optimizely | ||||||
* | ||||||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
* you may not use this file except in compliance with the License. | ||||||
|
@@ -25,6 +25,8 @@ namespace OptimizelySDK | |||||
{ | ||||||
public class ProjectConfig | ||||||
{ | ||||||
public const string RESERVED_ATTRIBUTE_PREFIX = "$opt_"; | ||||||
|
||||||
/// <summary> | ||||||
/// Version of the datafile. | ||||||
/// </summary> | ||||||
|
@@ -53,6 +55,11 @@ public class ProjectConfig | |||||
/// </summary> | ||||||
public bool AnonymizeIP { get; set; } | ||||||
|
||||||
/// <summary> | ||||||
/// Bot filtering flag. | ||||||
/// </summary> | ||||||
public bool? BotFiltering { get; set; } | ||||||
|
||||||
//========================= Mappings =========================== | ||||||
|
||||||
/// <summary> | ||||||
|
@@ -552,5 +559,32 @@ public Rollout GetRolloutFromId(string rolloutId) | |||||
ErrorHandler.HandleError(new Exceptions.InvalidRolloutException("Provided rollout is not in datafile.")); | ||||||
return new Rollout(); | ||||||
} | ||||||
|
||||||
/// <summary> | ||||||
/// Get attribute ID for the provided attribute key | ||||||
/// </summary> | ||||||
/// <param name="attributeKey">Key of the Attribute</param> | ||||||
/// <returns>Attribute ID corresponding to the provided attribute key. Attribute key if it is a reserved attribute</returns> | ||||||
public string GetAttributeId(string attributeKey) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add unit tests for this new module There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Already covered here:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, good. GitHub does not render large files by default and hence missed it. |
||||||
{ | ||||||
|
||||||
var hasReservedPrefix = attributeKey.StartsWith(RESERVED_ATTRIBUTE_PREFIX); | ||||||
|
||||||
if (_AttributeKeyMap.ContainsKey(attributeKey)) | ||||||
{ | ||||||
var attribute = _AttributeKeyMap[attributeKey]; | ||||||
if (hasReservedPrefix) | ||||||
Logger.Log(LogLevel.WARN, $@"Attribute {attributeKey} unexpectedly has reserved prefix {RESERVED_ATTRIBUTE_PREFIX}; using attribute ID instead of reserved attribute name."); | ||||||
|
||||||
return attribute.Id; | ||||||
} | ||||||
else if (hasReservedPrefix) | ||||||
{ | ||||||
return attributeKey; | ||||||
} | ||||||
|
||||||
Logger.Log(LogLevel.ERROR, $@"Attribute key ""{attributeKey}"" is not in datafile."); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit. Just single pair of double quotes should be good right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In verbatim strings, 2 double-quotes are used as an escape character like double backslashes in normal strings. The final string will contain attribute key in single double-quotes. |
||||||
return null; | ||||||
} | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit. This test is no different from the previous test. Let's have a separate example. Like lets test for $opt_reserved_prefix_attribute