Repository: getredash/redash

Issue:
Add events to track embeds usage

Add events to track embeds usage



Discussion:
(Remember to record the http-referrer as well)

(Remember to record the http-referrer as well)


Resolving patch:
diff --git a/redash/handlers/base.py b/redash/handlers/base.py
--- a/redash/handlers/base.py
+++ b/redash/handlers/base.py
@@ -5,7 +5,7 @@
 from peewee import DoesNotExist
 
 from redash import settings
-from redash.tasks import record_event
+from redash.tasks import record_event as record_event_task
 from redash.models import ApiUser
 from redash.authentication import current_org
 
@@ -33,26 +33,30 @@ def current_org(self):
         return current_org._get_current_object()
 
     def record_event(self, options):
-        if isinstance(self.current_user, ApiUser):
-            options.update({
-                'api_key': self.current_user.name,
-                'org_id': self.current_org.id
-            })
-        else:
-            options.update({
-                'user_id': self.current_user.id,
-                'org_id': self.current_org.id
-            })
+        record_event(self.current_org, self.current_user, options)
 
+
+def record_event(org, user, options):
+    if isinstance(user, ApiUser):
+        options.update({
+            'api_key': user.name,
+            'org_id': org.id
+        })
+    else:
         options.update({
-            'user_agent': request.user_agent.string,
-            'ip': request.remote_addr
+            'user_id': user.id,
+            'org_id': org.id
         })
 
-        if 'timestamp' not in options:
-            options['timestamp'] = int(time.time())
+    options.update({
+        'user_agent': request.user_agent.string,
+        'ip': request.remote_addr
+    })
+
+    if 'timestamp' not in options:
+        options['timestamp'] = int(time.time())
 
-        record_event.delay(options)
+    record_event_task.delay(options)
 
 
 def require_fields(req, fields):
diff --git a/redash/handlers/embed.py b/redash/handlers/embed.py
--- a/redash/handlers/embed.py
+++ b/redash/handlers/embed.py
@@ -9,7 +9,7 @@
 from redash import serializers
 from redash.utils import json_dumps
 from redash.handlers import routes
-from redash.handlers.base import org_scoped_rule
+from redash.handlers.base import org_scoped_rule, record_event
 from redash.permissions import require_access, view_only
 from authentication import current_org
 
@@ -17,7 +17,6 @@
 @routes.route(org_scoped_rule('/embed/query/<query_id>/visualization/<visualization_id>'), methods=['GET'])
 @login_required
 def embed(query_id, visualization_id, org_slug=None):
-    # TODO: add event for embed access
     query = models.Query.get_by_id_and_org(query_id, current_org)
     require_access(query.groups, current_user, view_only)
     vis = query.visualizations.where(models.Visualization.id == visualization_id).first()
@@ -33,6 +32,15 @@ def embed(query_id, visualization_id, org_slug=None):
     else:
         abort(404, message="Visualization not found.")
 
+    record_event(current_org, current_user, {
+        'action': 'view',
+        'object_id': visualization_id,
+        'object_type': 'visualization',
+        'query_id': query_id,
+        'embed': True,
+        'referer': request.headers.get('Referer')
+    })
+
     client_config = {}
     client_config.update(settings.COMMON_CLIENT_CONFIG)
 
@@ -65,6 +73,15 @@ def public_dashboard(token, org_slug=None):
         'Cache-Control': 'no-cache, no-store, max-age=0, must-revalidate'
     }
 
+    record_event(current_org, current_user, {
+        'action': 'view',
+        'object_id': dashboard.id,
+        'object_type': 'dashboard',
+        'public': True,
+        'headless': 'embed' in request.args,
+        'referer': request.headers.get('Referer')
+    })
+
     response = render_template("public.html",
                                headless='embed' in request.args,
                                user=json.dumps(user),


Test patch: