Provide more context to calls to Segment.
Implementation for DE-1089. Centralize the definition of context into a single method. This is in common/djangoapps/track because the context is originally set there by middleware.
This commit is contained in:
@@ -6,7 +6,6 @@ Much of this file was broken out from views.py, previous history can be found th
|
||||
|
||||
import logging
|
||||
|
||||
import analytics
|
||||
from django.conf import settings
|
||||
from django.contrib.auth import authenticate, login as django_login
|
||||
from django.contrib.auth.decorators import login_required
|
||||
@@ -19,7 +18,6 @@ from django.views.decorators.http import require_http_methods
|
||||
from ratelimitbackend.exceptions import RateLimitException
|
||||
|
||||
from edxmako.shortcuts import render_to_response
|
||||
from eventtracking import tracker
|
||||
from openedx.core.djangoapps.user_authn.cookies import set_logged_in_cookies, refresh_jwt_cookies
|
||||
from openedx.core.djangoapps.user_authn.exceptions import AuthFailedError
|
||||
import openedx.core.djangoapps.external_auth.views
|
||||
@@ -34,6 +32,7 @@ from student.models import (
|
||||
)
|
||||
from student.views import send_reactivation_email_for_user
|
||||
from student.forms import send_password_reset_email_for_user
|
||||
from track import segment
|
||||
import third_party_auth
|
||||
from third_party_auth import pipeline, provider
|
||||
from util.json_request import JsonResponse
|
||||
@@ -274,37 +273,28 @@ def _track_user_login(user, request):
|
||||
"""
|
||||
Sends a tracking event for a successful login.
|
||||
"""
|
||||
if hasattr(settings, 'LMS_SEGMENT_KEY') and settings.LMS_SEGMENT_KEY:
|
||||
tracking_context = tracker.get_tracker().resolve_context()
|
||||
analytics.identify(
|
||||
user.id,
|
||||
{
|
||||
'email': request.POST['email'],
|
||||
'username': user.username
|
||||
},
|
||||
{
|
||||
# Disable MailChimp because we don't want to update the user's email
|
||||
# and username in MailChimp on every page load. We only need to capture
|
||||
# this data on registration/activation.
|
||||
'MailChimp': False
|
||||
}
|
||||
)
|
||||
|
||||
analytics.track(
|
||||
user.id,
|
||||
"edx.bi.user.account.authenticated",
|
||||
{
|
||||
'category': "conversion",
|
||||
'label': request.POST.get('course_id'),
|
||||
'provider': None
|
||||
},
|
||||
context={
|
||||
'ip': tracking_context.get('ip'),
|
||||
'Google Analytics': {
|
||||
'clientId': tracking_context.get('client_id')
|
||||
}
|
||||
}
|
||||
)
|
||||
segment.identify(
|
||||
user.id,
|
||||
{
|
||||
'email': request.POST.get('email'),
|
||||
'username': user.username
|
||||
},
|
||||
{
|
||||
# Disable MailChimp because we don't want to update the user's email
|
||||
# and username in MailChimp on every page load. We only need to capture
|
||||
# this data on registration/activation.
|
||||
'MailChimp': False
|
||||
}
|
||||
)
|
||||
segment.track(
|
||||
user.id,
|
||||
"edx.bi.user.account.authenticated",
|
||||
{
|
||||
'category': "conversion",
|
||||
'label': request.POST.get('course_id'),
|
||||
'provider': None
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
|
||||
@@ -6,7 +6,6 @@ import datetime
|
||||
import json
|
||||
import logging
|
||||
|
||||
import analytics
|
||||
import dogstats_wrapper as dog_stats_api
|
||||
from django.conf import settings
|
||||
from django.contrib.auth import login as django_login
|
||||
@@ -17,7 +16,6 @@ from django.db import transaction
|
||||
from django.dispatch import Signal
|
||||
from django.utils.translation import get_language
|
||||
from django.utils.translation import ugettext as _
|
||||
from eventtracking import tracker
|
||||
# Note that this lives in LMS, so this dependency should be refactored.
|
||||
from notification_prefs.views import enable_notifications
|
||||
from pytz import UTC
|
||||
@@ -44,6 +42,7 @@ from student.models import (
|
||||
create_comments_service_user,
|
||||
)
|
||||
from student.views import compose_and_send_activation_email
|
||||
from track import segment
|
||||
import third_party_auth
|
||||
from third_party_auth import pipeline, provider
|
||||
from third_party_auth.saml import SAP_SUCCESSFACTORS_SAML_KEY
|
||||
@@ -316,7 +315,6 @@ def _link_user_to_third_party_provider(
|
||||
def _track_user_registration(user, profile, params, third_party_provider):
|
||||
""" Track the user's registration. """
|
||||
if hasattr(settings, 'LMS_SEGMENT_KEY') and settings.LMS_SEGMENT_KEY:
|
||||
tracking_context = tracker.get_tracker().resolve_context()
|
||||
identity_args = [
|
||||
user.id,
|
||||
{
|
||||
@@ -332,7 +330,7 @@ def _track_user_registration(user, profile, params, third_party_provider):
|
||||
'country': text_type(profile.country),
|
||||
}
|
||||
]
|
||||
|
||||
# Provide additional context only if needed.
|
||||
if hasattr(settings, 'MAILCHIMP_NEW_USER_LIST_ID'):
|
||||
identity_args.append({
|
||||
"MailChimp": {
|
||||
@@ -340,9 +338,8 @@ def _track_user_registration(user, profile, params, third_party_provider):
|
||||
}
|
||||
})
|
||||
|
||||
analytics.identify(*identity_args)
|
||||
|
||||
analytics.track(
|
||||
segment.identify(*identity_args)
|
||||
segment.track(
|
||||
user.id,
|
||||
"edx.bi.user.account.registered",
|
||||
{
|
||||
@@ -350,12 +347,6 @@ def _track_user_registration(user, profile, params, third_party_provider):
|
||||
'label': params.get('course_id'),
|
||||
'provider': third_party_provider.name if third_party_provider else None
|
||||
},
|
||||
context={
|
||||
'ip': tracking_context.get('ip'),
|
||||
'Google Analytics': {
|
||||
'clientId': tracking_context.get('client_id')
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -181,8 +181,8 @@ class TestCreateAccount(SiteMixin, TestCase):
|
||||
"Microsites not implemented in this environment"
|
||||
)
|
||||
@override_settings(LMS_SEGMENT_KEY="testkey")
|
||||
@mock.patch('openedx.core.djangoapps.user_authn.views.register.analytics.track')
|
||||
@mock.patch('openedx.core.djangoapps.user_authn.views.register.analytics.identify')
|
||||
@mock.patch('openedx.core.djangoapps.user_authn.views.register.segment.track')
|
||||
@mock.patch('openedx.core.djangoapps.user_authn.views.register.segment.identify')
|
||||
def test_segment_tracking(self, mock_segment_identify, _):
|
||||
year = datetime.now().year
|
||||
year_of_birth = year - 14
|
||||
|
||||
Reference in New Issue
Block a user