From 059d0dfff3475cbaa17a8dc170406d2d2a943021 Mon Sep 17 00:00:00 2001 From: Julia Hansbrough Date: Wed, 10 Sep 2014 13:52:33 +0000 Subject: [PATCH] Login analytics --- common/djangoapps/student/views.py | 19 ++++++--- .../djangoapps/third_party_auth/pipeline.py | 39 +++++++++++++++++++ .../djangoapps/third_party_auth/settings.py | 1 + 3 files changed, 54 insertions(+), 5 deletions(-) diff --git a/common/djangoapps/student/views.py b/common/djangoapps/student/views.py index 34e216dc1b..381d17e733 100644 --- a/common/djangoapps/student/views.py +++ b/common/djangoapps/student/views.py @@ -1005,7 +1005,8 @@ def login_user(request, error=""): # pylint: disable-msg=too-many-statements,un "edx.bi.user.account.authenticated", { 'category': "conversion", - 'label': registration_course_id + 'label': registration_course_id, + 'provider': None }, context={ 'Google Analytics': { @@ -1469,17 +1470,25 @@ def create_account(request, post_override=None): # pylint: disable-msg=too-many if settings.FEATURES.get('SEGMENT_IO_LMS') and hasattr(settings, 'SEGMENT_IO_LMS_KEY'): tracking_context = tracker.get_tracker().resolve_context() analytics.identify(user.id, { - email: email, - username: username, + 'email': email, + 'username': username, }) + # If the user is registering via 3rd party auth, track which provider they use + provider_name = None + if settings.FEATURES.get('ENABLE_THIRD_PARTY_AUTH') and pipeline.running(request): + running_pipeline = pipeline.get(request) + current_provider = provider.Registry.get_by_backend_name(running_pipeline.get('backend')) + provider_name = current_provider.NAME + registration_course_id = request.session.get('registration_course_id') analytics.track( user.id, "edx.bi.user.account.registered", { - "category": "conversion", - "label": registration_course_id + 'category': 'conversion', + 'label': registration_course_id, + 'provider': provider_name }, context={ 'Google Analytics': { diff --git a/common/djangoapps/third_party_auth/pipeline.py b/common/djangoapps/third_party_auth/pipeline.py index 241a957483..dc9d021e5e 100644 --- a/common/djangoapps/third_party_auth/pipeline.py +++ b/common/djangoapps/third_party_auth/pipeline.py @@ -59,6 +59,8 @@ See http://psa.matiasaguirre.net/docs/pipeline.html for more docs. import random import string # pylint: disable-msg=deprecated-module +import analytics +from eventtracking import tracker from django.contrib.auth.models import User from django.core.urlresolvers import reverse @@ -322,6 +324,10 @@ def parse_query_params(strategy, response, *args, **kwargs): if not (auth_entry and auth_entry in _AUTH_ENTRY_CHOICES): raise AuthEntryError(strategy.backend, 'auth_entry missing or invalid') + # Note: We expect only one member of this dictionary to be `True` at any + # given time. If something changes this convention in the future, please look + # at the `login_analytics` function in this file as well to ensure logging + # is still done properly return { # Whether the auth pipeline entered from /dashboard. 'is_dashboard': auth_entry == AUTH_ENTRY_DASHBOARD, @@ -360,3 +366,36 @@ def redirect_to_supplementary_form(strategy, details, response, uid, is_dashboar if is_register and user_unset: return redirect('/register', name='register_user') + +@partial.partial +def login_analytics(*args, **kwargs): + event_name = None + + action_to_event_name = { + 'is_login': 'edx.bi.user.account.authenticated', + 'is_dashboard': 'edx.bi.user.account.linked' + } + + # Note: we assume only one of the `action` kwargs (is_dashboard, is_login) to be + # `True` at any given time + for action in action_to_event_name.keys(): + if kwargs.get(action): + event_name = action_to_event_name[action] + + if event_name is not None: + registration_course_id = kwargs['request'].session.get('registration_course_id') + tracking_context = tracker.get_tracker().resolve_context() + analytics.track( + kwargs['user'].id, + event_name, + { + 'category': "conversion", + 'label': registration_course_id, + 'provider': getattr(kwargs['backend'], 'name') + }, + context={ + 'Google Analytics': { + 'clientId': tracking_context.get('client_id') + } + } + ) diff --git a/common/djangoapps/third_party_auth/settings.py b/common/djangoapps/third_party_auth/settings.py index bb4fc71415..07e46a3bed 100644 --- a/common/djangoapps/third_party_auth/settings.py +++ b/common/djangoapps/third_party_auth/settings.py @@ -109,6 +109,7 @@ def _set_global_settings(django_settings): 'social.pipeline.social_auth.associate_user', 'social.pipeline.social_auth.load_extra_data', 'social.pipeline.user.user_details', + 'third_party_auth.pipeline.login_analytics', ) # We let the user specify their email address during signup.