Skip to content

Implement IP Anonymization #87

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

Merged
merged 2 commits into from
Oct 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions optimizely/event_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class EventParams(object):
SOURCE_SDK_TYPE = 'client_name'
SOURCE_SDK_VERSION = 'client_version'
CUSTOM = 'custom'
ANONYMIZE_IP = 'anonymize_ip'

def _add_attributes(self, attributes):
""" Add attribute(s) information to the event.
Expand Down Expand Up @@ -172,6 +173,11 @@ def _add_visitor(self, user_id):
visitor[self.EventParams.SNAPSHOTS] = []
self.params[self.EventParams.USERS].append(visitor)

def _add_anonymize_ip(self):
""" Add IP anonymization bool to the event """

self.params[self.EventParams.ANONYMIZE_IP] = self.config.get_anonymize_ip_value()

def _add_common_params(self, user_id, attributes):
""" Add params which are used same in both conversion and impression events.

Expand All @@ -184,6 +190,7 @@ def _add_common_params(self, user_id, attributes):
self._add_visitor(user_id)
self._add_attributes(attributes)
self._add_source()
self._add_anonymize_ip()

def _add_required_params_for_impression(self, experiment, variation_id):
""" Add parameters that are required for the impression event to register.
Expand Down
10 changes: 10 additions & 0 deletions optimizely/project_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def __init__(self, datafile, logger, error_handler):
self.audiences = config.get('audiences', [])
self.feature_flags = config.get('featureFlags', [])
self.rollouts = config.get('rollouts', [])
self.anonymize_ip = config.get('anonymizeIP', False)

# Utility maps for quick lookup
self.group_id_map = self._generate_key_map(self.groups, 'id', entities.Group)
Expand Down Expand Up @@ -566,3 +567,12 @@ def get_forced_variation(self, experiment_key, user_id):
'Variation "%s" is mapped to experiment "%s" and user "%s" in the forced variation map'
% (variation.key, experiment_key, user_id))
return variation

def get_anonymize_ip_value(self):
""" Gets the anonymize IP value.

Returns:
A boolean value that indicates if the IP should be anonymized.
"""

return self.anonymize_ip
11 changes: 8 additions & 3 deletions tests/test_event_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ def test_create_impression_event(self):
}]
}],
'client_name': 'python-sdk',
'client_version': version.__version__
'client_version': version.__version__,
'anonymize_ip': False,
}

with mock.patch('time.time', return_value=42.123), \
Expand Down Expand Up @@ -122,7 +123,8 @@ def test_create_impression_event__with_attributes(self):
}]
}],
'client_name': 'python-sdk',
'client_version': version.__version__
'client_version': version.__version__,
'anonymize_ip': False
}

with mock.patch('time.time', return_value=42.123), \
Expand Down Expand Up @@ -167,7 +169,8 @@ def test_create_conversion_event__with_attributes(self):
}]
}],
'client_name': 'python-sdk',
'client_version': version.__version__
'client_version': version.__version__,
'anonymize_ip': False
}

with mock.patch('time.time', return_value=42.123), \
Expand Down Expand Up @@ -220,6 +223,7 @@ def test_create_conversion_event__with_event_tags(self):
}],
'account_id': '12001',
'client_name': 'python-sdk',
'anonymize_ip': False
}

with mock.patch('time.time', return_value=42.123), \
Expand Down Expand Up @@ -274,6 +278,7 @@ def test_create_conversion_event__with_invalid_event_tags(self):
}],
'account_id': '12001',
'client_name': 'python-sdk',
'anonymize_ip': False
}

with mock.patch('time.time', return_value=42.123), \
Expand Down
30 changes: 20 additions & 10 deletions tests/test_optimizely.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ def test_activate(self):
}]
}],
'client_version': version.__version__,
'client_name': 'python-sdk'
'client_name': 'python-sdk',
'anonymize_ip': False
}
mock_decision.assert_called_once_with(
self.project_config.get_experiment_from_key('test_experiment'), 'test_user', None
Expand Down Expand Up @@ -209,7 +210,8 @@ def test_activate__with_attributes__audience_match(self):
}]
}],
'client_version': version.__version__,
'client_name': 'python-sdk'
'client_name': 'python-sdk',
'anonymize_ip': False
}
mock_get_variation.assert_called_once_with(self.project_config.get_experiment_from_key('test_experiment'),
'test_user', {'test_attribute': 'test_value'})
Expand Down Expand Up @@ -256,7 +258,8 @@ def test_activate__with_attributes__audience_match__forced_bucketing(self):
}]
}],
'client_version': version.__version__,
'client_name': 'python-sdk'
'client_name': 'python-sdk',
'anonymize_ip': False
}

self.assertEqual(1, mock_dispatch_event.call_count)
Expand Down Expand Up @@ -368,7 +371,8 @@ def test_track__with_attributes(self):
}]
}],
'client_version': version.__version__,
'client_name': 'python-sdk'
'client_name': 'python-sdk',
'anonymize_ip': False
}
mock_get_variation.assert_called_once_with(self.project_config.get_experiment_from_key('test_experiment'),
'test_user', {'test_attribute': 'test_value'})
Expand Down Expand Up @@ -446,7 +450,8 @@ def test_track__with_event_tags(self):
}],
}],
'client_version': version.__version__,
'client_name': 'python-sdk'
'client_name': 'python-sdk',
'anonymize_ip': False
}
mock_get_variation.assert_called_once_with(self.project_config.get_experiment_from_key('test_experiment'),
'test_user', {'test_attribute': 'test_value'})
Expand Down Expand Up @@ -499,7 +504,8 @@ def test_track__with_event_tags_revenue(self):
'client_name': 'python-sdk',
'project_id': '111001',
'client_version': version.__version__,
'account_id': '12001'
'account_id': '12001',
'anonymize_ip': False
}
mock_get_variation.assert_called_once_with(self.project_config.get_experiment_from_key('test_experiment'),
'test_user', {'test_attribute': 'test_value'})
Expand Down Expand Up @@ -583,7 +589,8 @@ def test_track__with_event_tags__forced_bucketing(self):
}],
}],
'client_version': version.__version__,
'client_name': 'python-sdk'
'client_name': 'python-sdk',
'anonymize_ip': False
}

self.assertEqual(1, mock_dispatch_event.call_count)
Expand Down Expand Up @@ -633,7 +640,8 @@ def test_track__with_deprecated_event_value(self):
}],
}],
'client_version': version.__version__,
'client_name': 'python-sdk'
'client_name': 'python-sdk',
'anonymize_ip': False
}
mock_get_variation.assert_called_once_with(self.project_config.get_experiment_from_key('test_experiment'),
'test_user', {'test_attribute': 'test_value'})
Expand Down Expand Up @@ -684,7 +692,8 @@ def test_track__with_invalid_event_tags(self):
'client_name': 'python-sdk',
'project_id': '111001',
'client_version': version.__version__,
'account_id': '12001'
'account_id': '12001',
'anonymize_ip': False
}
mock_get_variation.assert_called_once_with(self.project_config.get_experiment_from_key('test_experiment'),
'test_user', {'test_attribute': 'test_value'})
Expand Down Expand Up @@ -798,7 +807,8 @@ def test_is_feature_enabled__returns_true_if_user_is_bucketed_into_a_variation_o
}]
}],
'client_version': version.__version__,
'client_name': 'python-sdk'
'client_name': 'python-sdk',
'anonymize_ip': False,
}
# Check that impression event is sent
self.assertEqual(1, mock_dispatch_event.call_count)
Expand Down