diff --git a/lms/djangoapps/discussion/signals/handlers.py b/lms/djangoapps/discussion/signals/handlers.py index 542f3875bd..ffa68a401a 100644 --- a/lms/djangoapps/discussion/signals/handlers.py +++ b/lms/djangoapps/discussion/signals/handlers.py @@ -3,6 +3,7 @@ Signal handlers related to discussions. """ import logging +from django.contrib.sites.models import Site from django.dispatch import receiver from django_comment_common import signals @@ -28,6 +29,7 @@ def send_message(comment): 'comment_author_id': comment.user_id, 'comment_username': comment.username, 'comment_created_at': comment.created_at, + 'site_id': Site.objects.get_current().id, 'thread_id': thread.id, 'thread_title': thread.title, 'thread_username': thread.username, diff --git a/lms/djangoapps/discussion/tasks.py b/lms/djangoapps/discussion/tasks.py index 251152f363..43852d3a86 100644 --- a/lms/djangoapps/discussion/tasks.py +++ b/lms/djangoapps/discussion/tasks.py @@ -3,6 +3,7 @@ Defines asynchronous celery task for sending email notification (through edx-ace pertaining to new discussion forum comments. """ import logging +from urllib import urlencode from urlparse import urljoin from celery import task @@ -15,6 +16,7 @@ from edx_ace import ace from edx_ace.message import MessageType from edx_ace.recipient import Recipient from opaque_keys.edx.keys import CourseKey +from openedx.core.djangoapps.site_configuration.helpers import get_value from lms.djangoapps.django_comment_client.utils import permalink import lms.lib.comment_client as cc from lms.lib.comment_client.utils import merge_dict @@ -39,7 +41,7 @@ class ResponseNotification(MessageType): @task(base=LoggedTask, routing_key=ROUTING_KEY) def send_ace_message(context): context['course_id'] = CourseKey.from_string(context['course_id']) - + context['site'] = Site.objects.get(id=context['site_id']) if _should_send_message(context): thread_author = User.objects.get(id=context['thread_author_id']) message_context = _build_message_context(context) @@ -76,9 +78,10 @@ def _get_course_language(course_id): def _build_message_context(context): - message_context = get_base_template_context(Site.objects.get_current()) + message_context = get_base_template_context(context['site']) message_context.update(context) message_context['post_link'] = _get_thread_url(context) + message_context['ga_tracking_pixel_url'] = _generate_ga_pixel_url(context) return message_context @@ -89,4 +92,26 @@ def _get_thread_url(context): 'commentable_id': context['thread_commentable_id'], 'id': context['thread_id'], } - return urljoin(settings.LMS_ROOT_URL, permalink(thread_content)) + return urljoin(context['site'].domain, permalink(thread_content)) + + +def _generate_ga_pixel_url(context): + # used for analytics + query_params = { + 'v': '1', # version, required for GA + 't': 'event', # + 'ec': 'email', # event category + 'ea': 'edx.bi.email.opened', # event action: in this case, the user opened the email + 'tid': get_value("GOOGLE_ANALYTICS_TRACKING_ID", getattr(settings, "GOOGLE_ANALYTICS_TRACKING_ID", None)), # tracking ID to associate this link with our GA instance + 'uid': context['thread_author_id'], + 'cs': 'sailthru', # Campaign source - what sent the email + 'cm': 'email', # Campaign medium - how the content is being delivered + 'cn': 'triggered_discussionnotification', # Campaign name - human-readable name for this particular class of message + 'dp': '/email/ace/discussions/responsenotification/{0}/'.format(context['course_id']), # document path, used for drilling down into specific events + 'dt': 'Reply to {0} at {1}'.format(context['thread_title'], context['comment_created_at']), # document title, should match the title of the email + } + + return u"{url}?{params}".format( + url="https://www.google-analytics.com/collect", + params=urlencode(query_params) + ) diff --git a/lms/djangoapps/discussion/templates/discussion/edx_ace/response_notification/email/body.html b/lms/djangoapps/discussion/templates/discussion/edx_ace/response_notification/email/body.html index 2a1632784f..1b3ba81124 100644 --- a/lms/djangoapps/discussion/templates/discussion/edx_ace/response_notification/email/body.html +++ b/lms/djangoapps/discussion/templates/discussion/edx_ace/response_notification/email/body.html @@ -56,3 +56,7 @@ {% endblock %} + +{% block google_analytics_pixel %} + +{% endblock %} diff --git a/lms/djangoapps/discussion/tests/test_tasks.py b/lms/djangoapps/discussion/tests/test_tasks.py index cb2702f259..a28eb30821 100644 --- a/lms/djangoapps/discussion/tests/test_tasks.py +++ b/lms/djangoapps/discussion/tests/test_tasks.py @@ -16,6 +16,7 @@ from django_comment_common.models import ForumsConfig from django_comment_common.signals import comment_created from edx_ace.recipient import Recipient from lms.djangoapps.discussion.config.waffle import waffle, FORUM_RESPONSE_NOTIFICATIONS +from lms.djangoapps.discussion.tasks import _generate_ga_pixel_url from openedx.core.djangoapps.content.course_overviews.tests.factories import CourseOverviewFactory from openedx.core.djangoapps.schedules.template_context import get_base_template_context from student.tests.factories import CourseEnrollmentFactory, UserFactory @@ -140,8 +141,12 @@ class TaskTestCase(ModuleStoreTestCase): 'thread_title': 'thread-title', 'thread_username': self.thread_author.username, 'thread_commentable_id': 'thread-commentable-id', - 'post_link': urljoin(settings.LMS_ROOT_URL, mock_permalink.return_value), + 'post_link': urljoin(Site.objects.get_current().domain, mock_permalink.return_value), + 'site': Site.objects.get_current(), + 'site_id': Site.objects.get_current().id, }) + ga_tracking_pixel_url = _generate_ga_pixel_url(expected_message_context) + expected_message_context.update({'ga_tracking_pixel_url': ga_tracking_pixel_url}) expected_recipient = Recipient(self.thread_author.username, self.thread_author.email) actual_message = mock_ace_send.call_args_list[0][0][0] self.assertEqual(expected_message_context, actual_message.context)