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:
@@ -2,7 +2,6 @@ import datetime
|
||||
import logging
|
||||
import random
|
||||
|
||||
import analytics
|
||||
from django.db.models.signals import post_save
|
||||
from django.dispatch import receiver
|
||||
|
||||
@@ -17,6 +16,7 @@ from openedx.core.djangoapps.schedules.models import ScheduleExperience
|
||||
from openedx.core.djangoapps.schedules.content_highlights import course_has_highlights
|
||||
from openedx.core.djangoapps.theming.helpers import get_current_site
|
||||
from student.models import CourseEnrollment
|
||||
from track import segment
|
||||
from .config import CREATE_SCHEDULE_WAFFLE_FLAG
|
||||
from .models import Schedule, ScheduleConfig
|
||||
from .tasks import update_course_schedules
|
||||
@@ -53,7 +53,7 @@ def create_schedule(sender, **kwargs): # pylint: disable=unused-argument
|
||||
))
|
||||
|
||||
|
||||
def update_schedules_on_course_start_changed(sender, updated_course_overview, previous_start_date, **kwargs):
|
||||
def update_schedules_on_course_start_changed(sender, updated_course_overview, previous_start_date, **kwargs): # pylint: disable=unused-argument
|
||||
"""
|
||||
Updates all course schedules start and upgrade_deadline dates based off of
|
||||
the new course overview start date.
|
||||
@@ -136,9 +136,9 @@ def _should_randomly_suppress_schedule_creation(
|
||||
upgrade_deadline_str = None
|
||||
if upgrade_deadline:
|
||||
upgrade_deadline_str = upgrade_deadline.isoformat()
|
||||
analytics.track(
|
||||
segment.track(
|
||||
user_id=enrollment.user.id,
|
||||
event='edx.bi.schedule.suppressed',
|
||||
event_name='edx.bi.schedule.suppressed',
|
||||
properties={
|
||||
'course_id': unicode(enrollment.course_id),
|
||||
'experience_type': experience_type,
|
||||
|
||||
@@ -97,7 +97,7 @@ class CreateScheduleTests(SharedModuleStoreTestCase):
|
||||
self.assert_schedule_created(experience_type=ScheduleExperience.EXPERIENCES.course_updates)
|
||||
|
||||
@override_waffle_flag(CREATE_SCHEDULE_WAFFLE_FLAG, True)
|
||||
@patch('analytics.track')
|
||||
@patch('openedx.core.djangoapps.schedules.signals.segment.track')
|
||||
@patch('openedx.core.djangoapps.schedules.signals.random.random', return_value=0.2)
|
||||
@ddt.data(
|
||||
(0, True),
|
||||
@@ -121,7 +121,7 @@ class CreateScheduleTests(SharedModuleStoreTestCase):
|
||||
else:
|
||||
self.assert_schedule_not_created()
|
||||
mock_track.assert_called_once()
|
||||
assert mock_track.call_args[1].get('event') == 'edx.bi.schedule.suppressed'
|
||||
assert mock_track.call_args[1].get('event_name') == 'edx.bi.schedule.suppressed'
|
||||
|
||||
@patch('openedx.core.djangoapps.schedules.signals.log.exception')
|
||||
@patch('openedx.core.djangoapps.schedules.signals.Schedule.objects.create')
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
API for managing user preferences.
|
||||
"""
|
||||
import logging
|
||||
import analytics
|
||||
from eventtracking import tracker
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
@@ -17,6 +15,7 @@ from pytz import common_timezones, common_timezones_set, country_timezones
|
||||
from six import text_type
|
||||
|
||||
from student.models import User, UserProfile
|
||||
from track import segment
|
||||
from ..errors import (
|
||||
UserAPIInternalError, UserAPIRequestError, UserNotFound, UserNotAuthorized,
|
||||
PreferenceValidationError, PreferenceUpdateError, CountryCodeError
|
||||
@@ -284,21 +283,13 @@ def _track_update_email_opt_in(user_id, organization, opt_in):
|
||||
|
||||
"""
|
||||
event_name = 'edx.bi.user.org_email.opted_in' if opt_in else 'edx.bi.user.org_email.opted_out'
|
||||
tracking_context = tracker.get_tracker().resolve_context()
|
||||
|
||||
analytics.track(
|
||||
segment.track(
|
||||
user_id,
|
||||
event_name,
|
||||
{
|
||||
'category': 'communication',
|
||||
'label': organization
|
||||
},
|
||||
context={
|
||||
'ip': tracking_context.get('ip'),
|
||||
'Google Analytics': {
|
||||
'clientId': tracking_context.get('client_id')
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -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