Commit 50e39982 by Bill DeRusha Committed by GitHub

Merge pull request #13768 from edx/bderusha/release-fix-2016-10-16

Fix for UTM parameter tracking when UTM not present
parents b8a02534 a36df010
...@@ -360,6 +360,49 @@ class TestCreateAccount(TestCase): ...@@ -360,6 +360,49 @@ class TestCreateAccount(TestCase):
self.assertIsNone(UserAttribute.get_user_attribute(user, REGISTRATION_UTM_PARAMETERS.get('utm_content'))) self.assertIsNone(UserAttribute.get_user_attribute(user, REGISTRATION_UTM_PARAMETERS.get('utm_content')))
self.assertIsNone(UserAttribute.get_user_attribute(user, REGISTRATION_UTM_CREATED_AT)) self.assertIsNone(UserAttribute.get_user_attribute(user, REGISTRATION_UTM_CREATED_AT))
@unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms')
def test_incomplete_utm_referral(self):
"""Verify that no referral is recorded when a cookie is not present."""
utm_cookie_name = 'edx.test.utm'
with mock.patch('student.models.RegistrationCookieConfiguration.current') as config:
instance = config.return_value
instance.utm_cookie_name = utm_cookie_name
utm_cookie = {
'utm_source': 'test-source',
'utm_medium': 'test-medium',
# No campaign
'utm_term': 'test-term',
'utm_content': 'test-content',
# No created at
}
self.client.cookies[utm_cookie_name] = json.dumps(utm_cookie)
user = self.create_account_and_fetch_profile().user
self.assertEqual(
UserAttribute.get_user_attribute(user, REGISTRATION_UTM_PARAMETERS.get('utm_source')),
utm_cookie.get('utm_source')
)
self.assertEqual(
UserAttribute.get_user_attribute(user, REGISTRATION_UTM_PARAMETERS.get('utm_medium')),
utm_cookie.get('utm_medium')
)
self.assertEqual(
UserAttribute.get_user_attribute(user, REGISTRATION_UTM_PARAMETERS.get('utm_term')),
utm_cookie.get('utm_term')
)
self.assertEqual(
UserAttribute.get_user_attribute(user, REGISTRATION_UTM_PARAMETERS.get('utm_content')),
utm_cookie.get('utm_content')
)
self.assertIsNone(
UserAttribute.get_user_attribute(user, REGISTRATION_UTM_PARAMETERS.get('utm_campaign'))
)
self.assertIsNone(
UserAttribute.get_user_attribute(user, REGISTRATION_UTM_CREATED_AT)
)
@ddt.ddt @ddt.ddt
class TestCreateAccountValidation(TestCase): class TestCreateAccountValidation(TestCase):
......
...@@ -1903,25 +1903,25 @@ def record_utm_registration_attribution(request, user): ...@@ -1903,25 +1903,25 @@ def record_utm_registration_attribution(request, user):
utm_cookie = request.COOKIES.get(utm_cookie_name) utm_cookie = request.COOKIES.get(utm_cookie_name)
if user and utm_cookie: if user and utm_cookie:
utm = json.loads(utm_cookie) utm = json.loads(utm_cookie)
for utm_parameter in REGISTRATION_UTM_PARAMETERS: for utm_parameter_name in REGISTRATION_UTM_PARAMETERS:
UserAttribute.set_user_attribute( utm_parameter = utm.get(utm_parameter_name)
user, if utm_parameter:
REGISTRATION_UTM_PARAMETERS.get(utm_parameter), UserAttribute.set_user_attribute(
utm.get(utm_parameter) user,
) REGISTRATION_UTM_PARAMETERS.get(utm_parameter_name),
utm_parameter
)
created_at_unixtime = utm.get('created_at') created_at_unixtime = utm.get('created_at')
if created_at_unixtime: if created_at_unixtime:
# We divide by 1000 here because the javascript timestamp generated is in milliseconds not seconds. # We divide by 1000 here because the javascript timestamp generated is in milliseconds not seconds.
# PYTHON: time.time() => 1475590280.823698 # PYTHON: time.time() => 1475590280.823698
# JS: new Date().getTime() => 1475590280823 # JS: new Date().getTime() => 1475590280823
created_at_datetime = datetime.datetime.fromtimestamp(int(created_at_unixtime) / float(1000), tz=UTC) created_at_datetime = datetime.datetime.fromtimestamp(int(created_at_unixtime) / float(1000), tz=UTC)
else: UserAttribute.set_user_attribute(
created_at_datetime = None user,
UserAttribute.set_user_attribute( REGISTRATION_UTM_CREATED_AT,
user, created_at_datetime
REGISTRATION_UTM_CREATED_AT, )
created_at_datetime
)
def record_registration_attributions(request, user): def record_registration_attributions(request, user):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment