Skip to content

Commit 7dac89e

Browse files
author
awb99
committed
analytics move in
1 parent 2dd292c commit 7dac89e

File tree

6 files changed

+150
-0
lines changed

6 files changed

+150
-0
lines changed

README-analytics.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Google Analytics
2+
3+
Some web app metrics are useful to be tracked.
4+
5+
## google analytics
6+
7+
- dashboard: https://analytics.google.com/analytics/web/
8+
- how to add credentials: https://support.google.com/analytics/answer/9306384
9+
10+
11+
## test
12+
- To test if tracking is working, realtime users should appear on: https://analytics.google.com/analytics/web/?utm_source=marketingplatform.google.com&utm_medium=et&utm_campaign=marketingplatform.google.com%2Fabout%2Fanalytics%2F#/realtime/rt-overview/a154548494w218059052p208029910/
13+
- post requests to google will be in browser console window
14+
- Ghostery ad blocker will by default suppress google analytics.
15+
16+
## development
17+
18+
- https://developers.google.com/analytics/devguides/collection/gtagjs/custom-dims-mets
19+
- upgrade to gtag: https://developers.google.com/analytics/devguides/collection/gtagjs/migration
20+
21+
22+
## Piwik - free/libre analytics platform
23+
- JavaScript tracking client
24+
- https://piwik.org
25+
- a possible option?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{:name "ui-analytics"
2+
; build
3+
:lazy false
4+
:cljs-namespace [frontend.analytics.google-tag]
5+
:cljs-ns-bindings {'frontend.analytics.google-tag {'frontend.analytics.google-tag frontend.analytics.google-tag/send-event
6+
}}
7+
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
(ns frontend.analytics.events
2+
(:require
3+
[taoensso.timbre :refer-macros [debug info warn error]]
4+
[re-frame.core :refer [reg-event-db]]
5+
[frontend.analytics.google-tag :refer [send-event]]))
6+
7+
(reg-event-db
8+
:ga/init
9+
(fn [db [_]]
10+
(let [{:keys [enabled id debug?]} (get-in db [:config :google-analytics])]
11+
(if enabled
12+
(do
13+
(info "ga init id: " id " debug?: " debug?)
14+
;(ga-init id debug?)
15+
)
16+
(debug "google analytics disabled.")))
17+
db))
18+
19+
(reg-event-db
20+
:ga/event
21+
(fn [db [_ {:keys [category action label value]}]]
22+
(let [{:keys [enabled]} (get-in db [:config :google-analytics])
23+
data {:event_category category
24+
:event_label label
25+
:value value}]
26+
(when enabled
27+
(info "ga send event" category)
28+
;(gtag "event" (name category)); label value (clj->js fields-object)
29+
(send-event action data)))
30+
db))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
(ns frontend.analytics.google-tag
2+
(:require
3+
[taoensso.timbre :refer [debug info warn error]]))
4+
5+
;The syntax is :async true for tags that don't have a value, but you'll need to ensure that the :mode is set to :html. So:
6+
; (html {:mode :html} [:script {:async true}])
7+
; Note that if you use the html5 macro, the mode will automatically be set when rendering.
8+
9+
(defn script-cljs [id]
10+
[:script {:async true ; async not rendered. see: https://github.com/weavejester/hiccup/issues/182
11+
:type "text/javascript"
12+
:src (str "https://www.googletagmanager.com/gtag/js?id=" id)}])
13+
14+
(defn script-js [id]
15+
[:div
16+
(str "<script async src='https://www.googletagmanager.com/gtag/js?id=" id "' type= 'text/javascript'> </script>")])
17+
18+
(defn script-tag-src
19+
[google-analytics-config]
20+
(let [{:keys [enabled id]} google-analytics-config]
21+
(if (and enabled id)
22+
(do (debug "google analytics starting with google id: " id)
23+
(script-cljs id)
24+
#_(script-js id))
25+
(do
26+
(debug "no google analytics config!")
27+
[:div {:class "no-google-analytics-config-tag"}]))))
28+
29+
(defn script-tag-config [google-analytics-config]
30+
(let [{:keys [enabled id]} google-analytics-config]
31+
(if (and enabled id)
32+
[:script
33+
(str "window.dataLayer = window.dataLayer || [];
34+
function gtag(){dataLayer.push(arguments);}
35+
gtag('js', new Date());
36+
gtag('config', '" id "', {cookie_flags: 'SameSite=None;Secure' } );") ; https://stackoverflow.com/questions/62569419/how-to-set-secure-attribute-of-the-cookies-used-by-google-analytics-global-sit
37+
]
38+
[:div {:class "no-google-analytics-config-config"}])))
39+
40+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(ns frontend.analytics.google-tag
2+
(:require
3+
[taoensso.timbre :refer-macros [debug info warn error]]))
4+
5+
; gtag("event", "sign_up", {"method": "email" });
6+
(defn send-event [action data]
7+
(let [datajs (clj->js data)]
8+
(info "ga event" action data)
9+
(js/gtag "event" action datajs)))
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
(ns frontend.tooltip
2+
(:require
3+
[reagent.core :as r]))
4+
5+
; stolen from:
6+
; https://www.creative-tim.com/learning-lab/tailwind-starter-kit/documentation/react/popovers/left
7+
8+
; ref handling from:
9+
; https://gist.github.com/pesterhazy/4d9df2edc303e5706d547aeabe0e17e1
10+
11+
(defn box-with-title [{:keys [title color]} & children]
12+
[:div
13+
[:div {:class (str "bg-" color "-600 text-white opacity-75 font-semibold p-3 mb-0 border-b border-solid border-gray-200 uppercase rounded-t-lg")}
14+
title]
15+
[:div {:class "text-white p-3"}
16+
children]])
17+
18+
(defn with-tooltip [text & children]
19+
(let [showing? (r/atom false)
20+
el-parent (r/atom nil)
21+
el-tooltip (r/atom nil)
22+
open-tooltip (fn [& args]
23+
(reset! showing? true))
24+
close-tooltip (fn [&args]
25+
(reset! showing? false))]
26+
(fn [text & children]
27+
(into [:div.inline-block {:on-mouse-enter open-tooltip
28+
:on-mouse-leave close-tooltip
29+
:ref #(reset! el-parent %)}
30+
(when @showing?
31+
[:div {:style {:top "40px"
32+
;:left "10px"
33+
}
34+
:class "absolute bg-blue-300 p-2 top-3 border-0 mr-3 block z-50 font-normal leading-normal text-sm max-w-xs text-left no-underline break-words rounded-lg"
35+
:ref #(reset! el-tooltip %)}
36+
text])]
37+
children))))
38+

0 commit comments

Comments
 (0)