Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Commit

Permalink
Bugfix: Put event names back; better guards on access to dataLayer.
Browse files Browse the repository at this point in the history
If a course author has modified base.html, then the setup to include
the Google Tag Manager content and the dataLayer variable will not be
present.  Event generation needs to guard against this.
	Change on 2014/02/21 by mgainer <mgainer@google.com>
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=61967978
  • Loading branch information
psimakov committed Feb 22, 2014
1 parent a50df53 commit 1842c3c
Show file tree
Hide file tree
Showing 21 changed files with 133 additions and 78 deletions.
43 changes: 33 additions & 10 deletions coursebuilder/assets/lib/activity-generic-1.3.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,23 @@ var eventXsrfToken = '';
var assessmentXsrfToken = '';

function gcbTagEventAudit(data_dict, name) {
gcbAudit(gcbCanPostTagEvents, data_dict, 'event-tag-' + name, true);
gcbAudit(gcbCanPostTagEvents, data_dict, 'tag-' + name, true);
}

function gcbPageEventAudit(data_dict, name) {
gcbAudit(gcbCanPostPageEvents, data_dict, name, false);
}

function gcbActivityAudit(data_dict) {
gcbAudit(gcbCanPostEvents, data_dict, 'event-attempt-activity', true);
gcbAudit(gcbCanPostEvents, data_dict, 'attempt-activity', true);
}

function gcbLessonAudit(data_dict) {
gcbAudit(gcbCanPostEvents, data_dict, 'event-attempt-lesson', true);
gcbAudit(gcbCanPostEvents, data_dict, 'attempt-lesson', true);
}

function gcbAssessmentAudit(data_dict) {
gcbAudit(gcbCanPostEvents, data_dict, 'event-attempt-assessment', true);
gcbAudit(gcbCanPostEvents, data_dict, 'attempt-assessment', true);
}

function gcbAudit(can_post, data_dict, source, is_async) {
Expand All @@ -79,16 +79,39 @@ function gcbAudit(can_post, data_dict, source, is_async) {
});
}

// ----------------------------------------------------------------------
// Report to the Google Tag manager, if it's configured. The 'dataLayer'
// object is hooked to override the normal array's 'push()' with a handler.
// This is the only place in CB that will emit 'event' as a name into the
// dataLayer. Thus, this should be the only place that causes a well-
// behaved set of tag rules to actually do anything. Note that we get
// here both on page load (from $(document).ready(), below), as well as
// from the various on-page events.
var tmp = {}
$.extend(tmp, data_dict, {'event': 'gcb.' + source, 'is_async': is_async});
dataLayer.push(tmp);
if ('dataLayer' in window) {

// Translate event names to be a bit more regularized. This permits
// simpler rules in the tag manager (e.g., "{{event}} startswith
// 'gcb.page-'" means it's a page-level event. Being able to make this
// distinction is convenient for passing along facts to Google Analytics.
switch (source) {
case 'enter-page':
case 'exit-page':
source = 'page.' + source
default:
source = 'event.' + source
break;
}

// Always prefix our events so that clients can distinguish between our
// events (starting with 'gcb.'), Google Tag Manager events (start with
// 'gtm.'), and any events from their own content or other libraries they
// might be pulling in.
source = 'gcb.' + source

var tmp = {}
$.extend(tmp, data_dict, {'event': source, 'is_async': is_async});
dataLayer.push(tmp);
}
}

// Returns the value of a URL parameter, if it exists.
Expand Down Expand Up @@ -840,12 +863,12 @@ $(document).ready(function() {

// send 'enter-page' event to the server
try {
gcbPageEventAudit({}, 'page-enter');
gcbPageEventAudit({}, 'enter-page');
} catch (e){}

// hook click events of specific links
$('#lessonNotesLink').click(function(evt) {
gcbPageEventAudit({'href': evt.target.href}, 'link-follow');
gcbPageEventAudit({'href': evt.target.href}, 'click-link');
return true;
});

Expand Down Expand Up @@ -876,6 +899,6 @@ $(window).unload(function() {
// send 'visit-page' event to the server
try {
// duration is in milliseconds
gcbPageEventAudit({'duration': (new Date() - gcbBeginningOfTime)}, 'page-unload');
gcbPageEventAudit({'duration': (new Date() - gcbBeginningOfTime)}, 'exit-page');
} catch (e){}
});
2 changes: 1 addition & 1 deletion coursebuilder/controllers/assessments.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def update_assessment_transaction(
# Also record the event, which is useful for tracking multiple
# submissions and history.
models.EventEntity.record(
'event-submit-assessment', self.get_user(), transforms.dumps({
'submit-assessment', self.get_user(), transforms.dumps({
'type': 'assessment-%s' % assessment_type,
'values': new_answers, 'location': 'AnswerHandler'}))

Expand Down
6 changes: 3 additions & 3 deletions coursebuilder/controllers/lessons.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@
UNIT_PAGE_TYPE = 'unit'
ACTIVITY_PAGE_TYPE = 'activity'

TAGS_THAT_TRIGGER_BLOCK_COMPLETION = ['event-attempt-activity']
TAGS_THAT_TRIGGER_COMPONENT_COMPLETION = ['event-tag-assessment']
TAGS_THAT_TRIGGER_HTML_COMPLETION = ['event-attempt-lesson']
TAGS_THAT_TRIGGER_BLOCK_COMPLETION = ['attempt-activity']
TAGS_THAT_TRIGGER_COMPONENT_COMPLETION = ['tag-assessment']
TAGS_THAT_TRIGGER_HTML_COMPLETION = ['attempt-lesson']


def get_first_lesson(handler, unit_id):
Expand Down
10 changes: 5 additions & 5 deletions coursebuilder/modules/dashboard/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,11 @@ class ComputeQuestionStats(jobs.DurableJob):
class MultipleChoiceQuestionAggregator(object):
"""Class that aggregates submissions for multiple-choice questions."""

ATTEMPT_ACTIVITY = 'event-attempt-activity'
TAG_ASSESSMENT = 'event-tag-assessment'
ATTEMPT_LESSON = 'event-attempt-lesson'
SUBMIT_ASSESSMENT = 'event-submit-assessment'
ATTEMPT_ASSESSMENT = 'event-attempt-assessment'
ATTEMPT_ACTIVITY = 'attempt-activity'
TAG_ASSESSMENT = 'tag-assessment'
ATTEMPT_LESSON = 'attempt-lesson'
SUBMIT_ASSESSMENT = 'submit-assessment'
ATTEMPT_ASSESSMENT = 'attempt-assessment'
MC_QUESTION = 'McQuestion'
QUESTION_GROUP = 'QuestionGroup'
ACTIVITY_CHOICE = 'activity-choice'
Expand Down
2 changes: 1 addition & 1 deletion coursebuilder/tests/functional/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ def attempt_activity(browser, unit_id, lesson_id, index, answer, correct):
response, args = get_activity(browser, unit_id, lesson_id, {})

# Prepare activity submission event.
args['source'] = 'event-attempt-activity'
args['source'] = 'attempt-activity'
args['payload'] = {
'index': index,
'type': 'activity-choice',
Expand Down
2 changes: 1 addition & 1 deletion coursebuilder/tests/functional/test_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2070,7 +2070,7 @@ def test_activities(self):
lesson_id, response.body)

# Prepare activity submission event.
args['source'] = 'event-attempt-activity'
args['source'] = 'attempt-activity'
lesson_key = '%s.%s' % (unit_id, lesson_id)
assert lesson_key in activity_submissions
args['payload'] = activity_submissions[lesson_key]
Expand Down
16 changes: 9 additions & 7 deletions coursebuilder/views/activity.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
{% extends 'base_course.html' %}

{% block gcb_page_info %}
dataLayer.push({
'page_type': 'activity',
'unit_id': '{{ unit_id }}',
{% if lesson_id %}
'lesson_id': '{{ lesson_id }}',
{% endif %}
});
if ('gcbSetPageInfo' in window) {
gcbSetPageInfo({
'page_type': 'activity',
'unit_id': '{{ unit_id }}',
{% if lesson_id %}
'lesson_id': '{{ lesson_id }}',
{% endif %}
});
}
{% endblock %}

{% block subtitle %}
Expand Down
12 changes: 7 additions & 5 deletions coursebuilder/views/assessment.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
{% extends 'base_course.html' %}

{% block gcb_page_info %}
dataLayer.push({
'page_type': 'assessment',
'assessment_id': '{{ assessment_name }}',
'unit_id': '{{ unit_id }}',
});
if ('gcbSetPageInfo' in window) {
gcbSetPageInfo({
'page_type': 'assessment',
'assessment_id': '{{ assessment_name }}',
'unit_id': '{{ unit_id }}',
});
}
{% endblock %}

{% block subtitle %}
Expand Down
4 changes: 4 additions & 0 deletions coursebuilder/views/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@
'event': 'default', // Explicit value meaning not-really-set.
}];

function gcbSetPageInfo(dict) {
dataLayer.push(dict);
}

// Invoke page-specific block so that any calls to gcbReportPageInfo()
// are made before we hit the Tag Manager call, below.
{% block gcb_page_info %}
Expand Down
8 changes: 5 additions & 3 deletions coursebuilder/views/course.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{% extends 'base_course.html' %}

{% block gcb_page_info %}
dataLayer.push({
'page_type': 'course',
});
if ('gcbSetPageInfo' in window) {
gcbSetPageInfo({
'page_type': 'course',
});
}
{% endblock %}

{% block top_content %}
Expand Down
8 changes: 5 additions & 3 deletions coursebuilder/views/forum.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{% extends 'base_course.html' %}

{% block gcb_page_info %}
dataLayer.push({
'page_type': 'forum',
});
if ('gcbSetPageInfo' in window) {
gcbSetPageInfo({
'page_type': 'forum',
});
}
{% endblock %}

{% block subtitle %}
Expand Down
8 changes: 5 additions & 3 deletions coursebuilder/views/preview.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{% extends 'base_course.html' %}

{% block gcb_page_info %}
dataLayer.push({
'page_type': 'preview',
});
if ('gcbSetPageInfo' in window) {
gcbSetPageInfo({
'page_type': 'preview',
});
}
{% endblock %}

{% block top_content %}
Expand Down
8 changes: 5 additions & 3 deletions coursebuilder/views/register.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{% extends 'base_course.html' %}

{% block gcb_page_info %}
dataLayer.push({
'page_type': 'register',
});
if ('gcbSetPageInfo' in window) {
gcbSetPageInfo({
'page_type': 'register',
});
}
{% endblock %}

{% block top_content %}
Expand Down
12 changes: 7 additions & 5 deletions coursebuilder/views/review.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
{% extends 'base_course.html' %}

{% block gcb_page_info %}
dataLayer.push({
'page_type': 'review',
'unit_id': '{{ unit_id }}',
'review_step': '{{ review_step_key }}',
});
if ('gcbSetPageInfo' in window) {
gcbSetPageInfo({
'page_type': 'review',
'unit_id': '{{ unit_id }}',
'review_step': '{{ review_step_key }}',
});
}
{% endblock %}

{% block subtitle %}
Expand Down
10 changes: 6 additions & 4 deletions coursebuilder/views/review_dashboard.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{% extends 'base_course.html' %}

{% block gcb_page_info %}
dataLayer.push({
'page_type': 'review_dashboard',
'unit_id': '{{ unit_id }}'
});
if ('gcbSetPageInfo' in window) {
gcbSetPageInfo({
'page_type': 'review_dashboard',
'unit_id': '{{ unit_id }}'
});
}
{% endblock %}

{% block subtitle %}
Expand Down
10 changes: 6 additions & 4 deletions coursebuilder/views/reviewed_assessment_confirmation.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{% extends 'base_course.html' %}

{% block gcb_page_info %}
dataLayer.push({
'page_type': 'reviewed_assessment_confirmation',
'unit_id': '{{ unit_id }}',
});
if ('gcbSetPageInfo' in window) {
gcbSetPageInfo({
'page_type': 'reviewed_assessment_confirmation',
'unit_id': '{{ unit_id }}',
});
}
{% endblock %}

{% block top_content %}
Expand Down
8 changes: 5 additions & 3 deletions coursebuilder/views/student_profile.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{% extends 'base_course.html' %}

{% block gcb_page_info %}
dataLayer.push({
'page_type': 'student_profile',
});
if ('gcbSetPageInfo' in window) {
gcbSetPageInfo({
'page_type': 'student_profile',
});
}
{% endblock %}

{% block subtitle %}
Expand Down
10 changes: 6 additions & 4 deletions coursebuilder/views/test_confirmation.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{% extends 'base_course.html' %}

{% block gcb_page_info %}
dataLayer.push({
'page_type': 'test_confirmation',
'unit_id': '{{ unit_id }}',
});
if ('gcbSetPageInfo' in window) {
gcbSetPageInfo({
'page_type': 'test_confirmation',
'unit_id': '{{ unit_id }}',
});
}
{% endblock %}

{% block top_content %}
Expand Down
8 changes: 5 additions & 3 deletions coursebuilder/views/unenroll_confirmation.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{% extends 'base_course.html' %}

{% block gcb_page_info %}
dataLayer.push({
'page_type': 'unenroll_confirmation',
});
if ('gcbSetPageInfo' in window) {
gcbSetPageInfo({
'page_type': 'unenroll_confirmation',
});
}
{% endblock %}

{% block top_content %}
Expand Down
8 changes: 5 additions & 3 deletions coursebuilder/views/unenroll_confirmation_check.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{% extends 'base_course.html' %}

{% block gcb_page_info %}
dataLayer.push({
'page_type': 'unenroll_confirmation_check',
});
if ('gcbSetPageInfo' in window) {
gcbSetPageInfo({
'page_type': 'unenroll_confirmation_check',
});
}
{% endblock %}

{% block top_content %}
Expand Down
Loading

0 comments on commit 1842c3c

Please sign in to comment.