Files
edx-platform/common/djangoapps/track/segment.py
Kyle McCormick 151bd13666 Use full names for common.djangoapps imports; warn when using old style (#25477)
* Generate common/djangoapps import shims for LMS
* Generate common/djangoapps import shims for Studio
* Stop appending project root to sys.path
* Stop appending common/djangoapps to sys.path
* Import from common.djangoapps.course_action_state instead of course_action_state
* Import from common.djangoapps.course_modes instead of course_modes
* Import from common.djangoapps.database_fixups instead of database_fixups
* Import from common.djangoapps.edxmako instead of edxmako
* Import from common.djangoapps.entitlements instead of entitlements
* Import from common.djangoapps.pipline_mako instead of pipeline_mako
* Import from common.djangoapps.static_replace instead of static_replace
* Import from common.djangoapps.student instead of student
* Import from common.djangoapps.terrain instead of terrain
* Import from common.djangoapps.third_party_auth instead of third_party_auth
* Import from common.djangoapps.track instead of track
* Import from common.djangoapps.util instead of util
* Import from common.djangoapps.xblock_django instead of xblock_django
* Add empty common/djangoapps/__init__.py to fix pytest collection
* Fix pylint formatting violations
* Exclude import_shims/ directory tree from linting
2020-11-10 07:02:01 -05:00

72 lines
3.0 KiB
Python

"""
Wrapper methods for emitting events to Segment directly (rather than through tracking log events).
These take advantage of properties that are extracted from incoming requests by track middleware,
stored in tracking context objects, and extracted here to be passed to Segment as part of context
required by server-side events.
To use, call "from common.djangoapps.track import segment", then call segment.track() or segment.identify().
"""
import analytics
from django.conf import settings
from eventtracking import tracker
from six.moves.urllib.parse import urlunsplit
def track(user_id, event_name, properties=None, context=None):
"""
Wrapper for emitting Segment track event, including augmenting context information from middleware.
"""
if event_name is not None and hasattr(settings, 'LMS_SEGMENT_KEY') and settings.LMS_SEGMENT_KEY:
properties = properties or {}
segment_context = dict(context) if context else {}
tracking_context = tracker.get_tracker().resolve_context()
if 'ip' not in segment_context and 'ip' in tracking_context:
segment_context['ip'] = tracking_context.get('ip')
if ('Google Analytics' not in segment_context or 'clientId' not in segment_context['Google Analytics']) and 'client_id' in tracking_context:
segment_context['Google Analytics'] = {
'clientId': tracking_context.get('client_id')
}
if 'userAgent' not in segment_context and 'agent' in tracking_context:
segment_context['userAgent'] = tracking_context.get('agent')
path = tracking_context.get('path')
referer = tracking_context.get('referer')
page = tracking_context.get('page')
if path and not page:
# Try to put together a url from host and path, hardcoding the schema.
# (Segment doesn't care about the schema for GA, but will extract the host and path from the url.)
host = tracking_context.get('host')
if host:
parts = ("https", host, path, "", "")
page = urlunsplit(parts)
if path is not None or referer is not None or page is not None:
if 'page' not in segment_context:
segment_context['page'] = {}
if path is not None and 'path' not in segment_context['page']:
segment_context['page']['path'] = path
if referer is not None and 'referrer' not in segment_context['page']:
segment_context['page']['referrer'] = referer
if page is not None and 'url' not in segment_context['page']:
segment_context['page']['url'] = page
analytics.track(user_id, event_name, properties, segment_context)
def identify(user_id, properties, context=None):
"""
Wrapper for emitting Segment identify event.
"""
if hasattr(settings, 'LMS_SEGMENT_KEY') and settings.LMS_SEGMENT_KEY:
segment_context = dict(context) if context else {}
analytics.identify(user_id, properties, segment_context)