Example #1
0
    def enter_request_context(self, request):
        """
        Extract information from the request and add it to the tracking
        context.

        The following fields are injected into the context:

        * session - The Django session key that identifies the user's session.
        * user_id - The numeric ID for the logged in user.
        * username - The username of the logged in user.
        * ip - The IP address of the client.
        * host - The "SERVER_NAME" header, which should be the name of the server running this code.
        * agent - The client browser identification string.
        * path - The path part of the requested URL.
        * client_id - The unique key used by Google Analytics to identify a user
        """
        context = {
            "session": self.get_session_key(request),
            "user_id": self.get_user_primary_key(request),
            "username": self.get_username(request),
        }
        for header_name, context_key in META_KEY_TO_CONTEXT_KEY.iteritems():
            context[context_key] = request.META.get(header_name, "")

        # Google Analytics uses the clientId to keep track of unique visitors. A GA cookie looks like
        # this: _ga=GA1.2.1033501218.1368477899. The clientId is this part: 1033501218.1368477899.
        google_analytics_cookie = request.COOKIES.get("_ga")
        if google_analytics_cookie is None:
            context["client_id"] = None
        else:
            context["client_id"] = ".".join(google_analytics_cookie.split(".")[2:])

        context.update(contexts.course_context_from_url(request.build_absolute_uri()))

        tracker.get_tracker().enter_context(CONTEXT_NAME, context)
Example #2
0
    def enter_request_context(self, request):
        """
        Extract information from the request and add it to the tracking
        context.

        The following fields are injected in to the context:

        * session - The Django session key that identifies the user's session.
        * user_id - The numeric ID for the logged in user.
        * username - The username of the logged in user.
        * ip - The IP address of the client.
        * host - The "SERVER_NAME" header, which should be the name of the server running this code.
        * agent - The client browser identification string.
        * path - The path part of the requested URL.
        """
        context = {
            'session': self.get_session_key(request),
            'user_id': self.get_user_primary_key(request),
            'username': self.get_username(request),
        }
        for header_name, context_key in META_KEY_TO_CONTEXT_KEY.iteritems():
            context[context_key] = request.META.get(header_name, '')

        context.update(contexts.course_context_from_url(request.build_absolute_uri()))

        tracker.get_tracker().enter_context(
            CONTEXT_NAME,
            context
        )
Example #3
0
def user_track(request):
    """
    Log when POST call to "event" URL is made by a user. Uses request.REQUEST
    to allow for GET calls.

    GET or POST call should provide "event_type", "event", and "page" arguments.
    """
    try:  # TODO: Do the same for many of the optional META parameters
        username = request.user.username
    except:
        username = "******"

    page = request.REQUEST['page']

    with eventtracker.get_tracker().context('edx.course.browser', contexts.course_context_from_url(page)):
        context = eventtracker.get_tracker().resolve_context()
        event = {
            "username": username,
            "session": context.get('session', ''),
            "ip": _get_request_header(request, 'REMOTE_ADDR'),
            "event_source": "browser",
            "event_type": request.REQUEST['event_type'],
            "event": request.REQUEST['event'],
            "agent": _get_request_header(request, 'HTTP_USER_AGENT'),
            "page": page,
            "time": datetime.datetime.utcnow(),
            "host": _get_request_header(request, 'SERVER_NAME'),
            "context": context,
        }

    log_event(event)

    return HttpResponse('success')
Example #4
0
def user_track(request):
    """
    Log when POST call to "event" URL is made by a user.

    GET or POST call should provide "event_type", "event", and "page" arguments.
    """
    try:
        username = request.user.username
    except:
        username = "******"

    name = _get_request_value(request, 'event_type')
    data = _get_request_value(request, 'event', {})
    page = _get_request_value(request, 'page')

    if isinstance(data, six.string_types) and len(data) > 0:
        try:
            data = json.loads(data)
            _add_user_id_for_username(data)
        except ValueError:
            pass

    context_override = contexts.course_context_from_url(page)
    context_override['username'] = username
    context_override['event_source'] = 'browser'
    context_override['page'] = page

    with eventtracker.get_tracker().context('edx.course.browser', context_override):
        eventtracker.emit(name=name, data=data)

    return HttpResponse('success')
Example #5
0
 def assert_parses_course_id_from_url(self, format_string, course_id):
     self.assertEquals(
         contexts.course_context_from_url(format_string.format(course_id=course_id)),
         {
             'course_id': course_id,
             'org_id': self.ORG_ID
         }
     )
Example #6
0
 def assert_parses_course_id_from_url(self, format_string):
     self.assertEquals(
         contexts.course_context_from_url(format_string.format(course_id=self.COURSE_ID)),
         {
             'course_id': self.COURSE_ID,
             'org_id': self.ORG_ID
         }
     )
Example #7
0
 def assert_empty_context_for_url(self, url):
     self.assertEquals(
         contexts.course_context_from_url(url),
         {
             'course_id': '',
             'org_id': ''
         }
     )
Example #8
0
 def enter_course_context(self, request):
     """
     Extract course information from the request and add it to the
     tracking context.
     """
     tracker.get_tracker().enter_context(
         COURSE_CONTEXT_NAME,
         contexts.course_context_from_url(request.build_absolute_uri())
     )
Example #9
0
    def enter_request_context(self, request):
        """
        Extract information from the request and add it to the tracking
        context.

        The following fields are injected into the context:

        * session - The Django session key that identifies the user's session.
        * user_id - The numeric ID for the logged in user.
        * username - The username of the logged in user.
        * ip - The IP address of the client.
        * host - The "SERVER_NAME" header, which should be the name of the server running this code.
        * agent - The client browser identification string.
        * path - The path part of the requested URL.
        * client_id - The unique key used by Google Analytics to identify a user
        """
        context = {
            'session': self.get_session_key(request),
            'user_id': self.get_user_primary_key(request),
            'username': self.get_username(request),
            'ip': self.get_request_ip_address(request),
        }

        # If IONISx authentication is enabled, add IONISx context
        if settings.IONISX_AUTH:
            context.update({
                'ionisx_id': self.get_ionisx_user_id(request),
                'ionisx_session': self.get_ionisx_session(request),
            })

        for header_name, context_key in META_KEY_TO_CONTEXT_KEY.iteritems():
            context[context_key] = request.META.get(header_name, '')

        # Google Analytics uses the clientId to keep track of unique visitors. A GA cookie looks like
        # this: _ga=GA1.2.1033501218.1368477899. The clientId is this part: 1033501218.1368477899.
        google_analytics_cookie = request.COOKIES.get('_ga')
        if google_analytics_cookie is None:
            context['client_id'] = request.META.get('HTTP_X_EDX_GA_CLIENT_ID')
        else:
            context['client_id'] = '.'.join(google_analytics_cookie.split('.')[2:])

        context.update(contexts.course_context_from_url(request.build_absolute_uri()))

        tracker.get_tracker().enter_context(
            CONTEXT_NAME,
            context
        )
Example #10
0
    def enter_request_context(self, request):
        """
        Extract information from the request and add it to the tracking
        context.
        """
        context = {}
        context.update(contexts.course_context_from_url(request.build_absolute_uri()))
        try:
            context['user_id'] = request.user.pk
        except AttributeError:
            context['user_id'] = ''
            if settings.DEBUG:
                log.error('Cannot determine primary key of logged in user.')

        tracker.get_tracker().enter_context(
            CONTEXT_NAME,
            context
        )
Example #11
0
def user_track(request):
    """
    Log when POST call to "event" URL is made by a user. Uses request.REQUEST
    to allow for GET calls.

    GET or POST call should provide "event_type", "event", and "page" arguments.
    """
    try:  # TODO: Do the same for many of the optional META parameters
        username = request.user.username
    except:
        username = "******"

    try:
        scookie = request.META['HTTP_COOKIE']  # Get cookies
        scookie = ";".join([c.split('=')[1] for c in scookie.split(";") if "sessionid" in c]).strip()  # Extract session ID
    except:
        scookie = ""

    try:
        agent = request.META['HTTP_USER_AGENT']
    except:
        agent = ''

    page = request.REQUEST['page']

    with eventtracker.get_tracker().context('edx.course.browser', contexts.course_context_from_url(page)):
        event = {
            "username": username,
            "session": scookie,
            "ip": request.META['REMOTE_ADDR'],
            "event_source": "browser",
            "event_type": request.REQUEST['event_type'],
            "event": request.REQUEST['event'],
            "agent": agent,
            "page": page,
            "time": datetime.datetime.now(UTC),
            "host": request.META['SERVER_NAME'],
            "context": eventtracker.get_tracker().resolve_context(),
        }

    log_event(event)

    return HttpResponse('success')
Example #12
0
    def enter_request_context(self, request):
        """
        Extract information from the request and add it to the tracking
        context.

        The following fields are injected into the context:

        * session - The Django session key that identifies the user's session.
        * user_id - The numeric ID for the logged in user.
        * username - The username of the logged in user.
        * ip - The IP address of the client.
        * host - The "SERVER_NAME" header, which should be the name of the server running this code.
        * agent - The client browser identification string.
        * path - The path part of the requested URL.
        * client_id - The unique key used by Google Analytics to identify a user
        """
        context = {
            'session': self.get_session_key(request),
            'user_id': self.get_user_primary_key(request),
            'username': self.get_username(request),
            'ip': self.get_request_ip_address(request),
        }
        for header_name, context_key in META_KEY_TO_CONTEXT_KEY.iteritems():
            context[context_key] = request.META.get(header_name, '')

        # HTTP_USER_AGENT user might can contain the information that include latin1 characters
        # decoding this using latin1 scheme will prevent to raise UnicodeDecodeError when using
        # json.dumps for tracking purpose.
        context['agent'] = context['agent'].decode('latin1')
        # Google Analytics uses the clientId to keep track of unique visitors. A GA cookie looks like
        # this: _ga=GA1.2.1033501218.1368477899. The clientId is this part: 1033501218.1368477899.
        google_analytics_cookie = request.COOKIES.get('_ga')
        if google_analytics_cookie is None:
            context['client_id'] = request.META.get('HTTP_X_EDX_GA_CLIENT_ID')
        else:
            context['client_id'] = '.'.join(google_analytics_cookie.split('.')[2:])

        context.update(contexts.course_context_from_url(request.build_absolute_uri()))

        tracker.get_tracker().enter_context(
            CONTEXT_NAME,
            context
        )
Example #13
0
def task_track(request_info, task_info, event_type, event, page=None):
    """
    Logs tracking information for events occuring within celery tasks.

    The `event_type` is a string naming the particular event being logged,
    while `event` is a dict containing whatever additional contextual information
    is desired.

    The `request_info` is a dict containing information about the original
    task request.  Relevant keys are `username`, `ip`, `agent`, and `host`.
    While the dict is required, the values in it are not, so that {} can be
    passed in.

    In addition, a `task_info` dict provides more information about the current
    task, to be stored with the `event` dict.  This may also be an empty dict.

    The `page` parameter is optional, and allows the name of the page to
    be provided.
    """

    # supplement event information with additional information
    # about the task in which it is running.
    full_event = dict(event, **task_info)

    # All fields must be specified, in case the tracking information is
    # also saved to the TrackingLog model.  Get values from the task-level
    # information, or just add placeholder values.
    with eventtracker.get_tracker().context('edx.course.task', contexts.course_context_from_url(page)):
        event = {
            "username": request_info.get('username', 'unknown'),
            "ip": request_info.get('ip', 'unknown'),
            "event_source": "task",
            "event_type": event_type,
            "event": full_event,
            "agent": request_info.get('agent', 'unknown'),
            "page": page,
            "time": datetime.datetime.now(UTC),
            "host": request_info.get('host', 'unknown'),
            "context": eventtracker.get_tracker().resolve_context(),
        }

    log_event(event)
Example #14
0
def user_track(request):
    """
    Log when POST call to "event" URL is made by a user. Uses request.REQUEST
    to allow for GET calls.

    GET or POST call should provide "event_type", "event", and "page" arguments.
    """
    try:  # TODO: Do the same for many of the optional META parameters
        username = request.user.username
    except:
        username = "******"

    page = _get_request_value(request, 'page')

    with eventtracker.get_tracker().context('edx.course.browser', contexts.course_context_from_url(page)):
        context = eventtracker.get_tracker().resolve_context()
        event = {
            "username": username,
            "session": context.get('session', ''),
            "ip": _get_request_header(request, 'REMOTE_ADDR'),
            "referer": _get_request_header(request, 'HTTP_REFERER'),
            "accept_language": _get_request_header(request, 'HTTP_ACCEPT_LANGUAGE'),
            "event_source": "browser",
            "event_type": _get_request_value(request, 'event_type'),
            "event": _get_request_value(request, 'event'),
            "agent": _get_request_header(request, 'HTTP_USER_AGENT'),
            "page": page,
            "time": datetime.datetime.utcnow(),
            "host": _get_request_header(request, 'SERVER_NAME'),
            "context": context,
        }

    # Some duplicated fields are passed into event-tracking via the context by track.middleware.
    # Remove them from the event here since they are captured elsewhere.
    shim.remove_shim_context(event)

    log_event(event)

    return HttpResponse('success')
Example #15
0
    def enter_request_context(self, request):
        """
        Extract information from the request and add it to the tracking
        context.

        The following fields are injected into the context:

        * session - The Django session key that identifies the user's session.
        * user_id - The numeric ID for the logged in user.
        * username - The username of the logged in user.
        * ip - The IP address of the client.
        * host - The "SERVER_NAME" header, which should be the name of the server running this code.
        * agent - The client browser identification string.
        * path - The path part of the requested URL.
        * client_id - The unique key used by Google Analytics to identify a user
        """
        context = {
            'session': self.get_session_key(request),
            'user_id': self.get_user_primary_key(request),
            'username': self.get_username(request),
        }
        for header_name, context_key in META_KEY_TO_CONTEXT_KEY.iteritems():
            context[context_key] = request.META.get(header_name, '')

        # Google Analytics uses the clientId to keep track of unique visitors. A GA cookie looks like
        # this: _ga=GA1.2.1033501218.1368477899. The clientId is this part: 1033501218.1368477899.
        google_analytics_cookie = request.COOKIES.get('_ga')
        if google_analytics_cookie is None:
            context['client_id'] = None
        else:
            context['client_id'] = '.'.join(
                google_analytics_cookie.split('.')[2:])

        context.update(
            contexts.course_context_from_url(request.build_absolute_uri()))

        tracker.get_tracker().enter_context(CONTEXT_NAME, context)
Example #16
0
def user_track(request):
    """
    Log when POST call to "event" URL is made by a user. Uses request.REQUEST
    to allow for GET calls.

    GET or POST call should provide "event_type", "event", and "page" arguments.
    """
    # EDXFIX: 参照我们以前的track改,确保track日志一致
    try:  # TODO: Do the same for many of the optional META parameters
        username = request.user.username
    except:
        username = "******"

    sessionid = _get_session_id(request, True)
    event_type = _get_request_value(request, 'event_type')

    if 'event' in request.REQUEST:
        try:
            page = request.REQUEST['page']
            event_obj = request.REQUEST.get('event')
        except:
            page = ''
            event_obj = ''

        try:
            event_obj = json.loads(event_obj)
        except:
            pass
    else:
        page = ''
        try:
            event_obj = json.loads(request.raw_post_data)
            event_type = eventtracker.get_tracker().resolve_context()['path']
        except:
            event_obj = ''

    with eventtracker.get_tracker().context('edx.course.browser', contexts.course_context_from_url(page)):
        event = {
            "username": username,
            "session": sessionid,
            "referer": _get_request_header(request, 'HTTP_REFERER'),
            "origin_referer": request.session.get('referer'),
            "spam": request.COOKIES.get('spam') or request.GET.get('spam'),
            "ip": _get_request_header(request, 'REMOTE_ADDR'),
            "event_source": "browser",
            "event_type": event_type,
            "event": event_obj,
            "agent": _get_request_header(request, 'HTTP_USER_AGENT'),
            "page": page,
            "time": datetime.datetime.now(UTC),
            "host": _get_request_header(request, 'SERVER_NAME'),
            "context": eventtracker.get_tracker().resolve_context(),
        }

    # Some duplicated fields are passed into event-tracking via the context by track.middleware.
    # Remove them from the event here since they are captured elsewhere.
    shim.remove_shim_context(event)

    log_event(event)

    return HttpResponse('success')
 def assert_empty_context_for_url(self, url):
     self.assertEqual(contexts.course_context_from_url(url), {
         'course_id': '',
         'org_id': ''
     })