Skip to content
Snippets Groups Projects
Unverified Commit 1c6fff19 authored by Cal Herries's avatar Cal Herries Committed by GitHub
Browse files

Add snowplow tracking for search queries (#31467)

parent 40c92cc5
No related branches found
No related tags found
No related merge requests found
{
"$schema": "http://iglucentral.com/schemas/com.snowplowanalytics.self-desc/schema/jsonschema/1-0-0#",
"description": "Search events",
"self": {
"vendor": "com.example",
"name": "search",
"format": "jsonschema",
"version": "1-0-0"
},
"type": "object",
"properties": {
"event": {
"description": "Event name",
"type": "string",
"enum": ["new_search_query"],
"maxLength": 1024
},
"runtime_milliseconds": {
"description": "Number of milliseconds it took to successfully run the search query",
"type": "integer",
"minimum": 0,
"maximum": 2147483647
}
},
"required": ["runtime"],
"additionalProperties": true
}
......@@ -142,6 +142,7 @@
::database "1-0-0"
::instance "1-1-0"
::metabot "1-0-0"
::search "1-0-0"
::timeline "1-0-0"
::task "1-0-0"
::action "1-0-0"})
......@@ -203,6 +204,7 @@
::database-connection-failed ::database
::new-event-created ::timeline
::new-task-history ::task
::new-search-query ::search
::action-created ::action
::action-updated ::action
::action-deleted ::action
......
......@@ -5,6 +5,7 @@
[flatland.ordered.map :as ordered-map]
[honey.sql.helpers :as sql.helpers]
[medley.core :as m]
[metabase.analytics.snowplow :as snowplow]
[metabase.api.common :as api]
[metabase.db :as mdb]
[metabase.db.query :as mdb.query]
......@@ -515,12 +516,20 @@
table_db_id (s/maybe su/IntGreaterThanZero)
models (s/maybe models-schema)}
(api/check-valid-page-params mw.offset-paging/*limit* mw.offset-paging/*offset*)
(search (search-context
q
archived
table_db_id
models
mw.offset-paging/*limit*
mw.offset-paging/*offset*)))
(let [start-time (System/currentTimeMillis)
results (search (search-context
q
archived
table_db_id
models
mw.offset-paging/*limit*
mw.offset-paging/*offset*))
duration (- (System/currentTimeMillis) start-time)]
;; Only track global searches
(when (and (nil? models)
(nil? table_db_id)
(not archived))
(snowplow/track-event! ::snowplow/new-search-query api/*current-user-id* {:runtime-milliseconds duration}))
results))
(api/define-routes)
......@@ -3,6 +3,7 @@
[clojure.set :as set]
[clojure.string :as str]
[clojure.test :refer :all]
[metabase.analytics.snowplow-test :as snowplow-test]
[metabase.api.common :as api]
[metabase.api.search :as api.search]
[metabase.mbql.normalize :as mbql.normalize]
......@@ -724,3 +725,22 @@
;; we have this test here just to keep tracks this number to remind us to put effort
;; into keep this number as low as we can
(is (= 7 (call-count)))))))
(deftest snowplow-new-search-query-event-test
(testing "Send a snowplow event when a new global search query is made"
(snowplow-test/with-fake-snowplow-collector
(mt/user-http-request :crowberto :get 200 "search?q=test")
(is (=? {:data {"event" "new_search_query"
"runtime_milliseconds" pos?}
:user-id (str (mt/user->id :crowberto))}
(last (snowplow-test/pop-event-data-and-user-id!))))))
(testing "Don't send a snowplow event if the search isn't global"
(snowplow-test/with-fake-snowplow-collector
(mt/user-http-request :crowberto :get 200 "search" :q "test" :models "table")
(is (empty? (snowplow-test/pop-event-data-and-user-id!))))
(snowplow-test/with-fake-snowplow-collector
(mt/user-http-request :crowberto :get 200 "search" :q "test" :table_db_id (mt/id))
(is (empty? (snowplow-test/pop-event-data-and-user-id!))))
(snowplow-test/with-fake-snowplow-collector
(mt/user-http-request :crowberto :get 200 "search" :q "test" :archived true)
(is (empty? (snowplow-test/pop-event-data-and-user-id!))))))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment