Inject Amplitude init script properly

This commit is contained in:
Jeremy Dormitzer 2018-01-31 18:14:57 -07:00
parent 99ddcb33e3
commit 419aa55f14
No known key found for this signature in database
GPG Key ID: 04F17C0F5A32C320
2 changed files with 8 additions and 7 deletions

View File

@ -1,10 +1,10 @@
(ns looped-in.analytics
(:require [goog.html.SafeScript :as SafeScript]
[goog.string.Const :as Const]))
(:require [goog.dom :as dom]))
(goog-define amplitude-api-key "FAKE_API_KEY")
(def amplitude-init-code
"function(apiKey) {(function(e,t){var n=e.amplitude||{_q:[],_iq:{}};var r=t.createElement('script');r.type='text/javascript';r.async=true;r.src='https://cdn.amplitude.com/libs/amplitude-4.1.0-min.gz.js';r.onload=function(){if(e.amplitude.runQueuedFunctions){e.amplitude.runQueuedFunctions()}else{console.log('[Amplitude] Error: could not load SDK')}};var i=t.getElementsByTagName('script')[0];i.parentNode.insertBefore(r,i);function s(e,t){e.prototype[t]=function(){this._q.push([t].concat(Array.prototype.slice.call(arguments,0)));return this}}var o=function(){this._q=[];return this};var a=['add','append','clearAll','prepend','set','setOnce','unset'];for(var u=0;u<a.length;u++){s(o,a[u])}n.Identify=o;var c=function(){this._q=[];return this};var l=['setProductId','setQuantity','setPrice','setRevenueType','setEventProperties'];for(var p=0;p<l.length;p++){s(c,l[p])}n.Revenue=c;var d=['init','logEvent','logRevenue','setUserId','setUserProperties','setOptOut','setVersionName','setDomain','setDeviceId','setGlobalUserProperties','identify','clearUserProperties','setGroup','logRevenueV2','regenerateDeviceId','logEventWithTimestamp','logEventWithGroups','setSessionId'];function v(e){function t(t){e[t]=function(){ e._q.push([t].concat(Array.prototype.slice.call(arguments,0)))}}for(var n=0;n<d.length;n++){t(d[n])}}v(n);n.getInstance=function(e){ e=(!e||e.length===0?'$default_instance':e).toLowerCase();if(!n._iq.hasOwnProperty(e)){n._iq[e]={_q:[]};v(n._iq[e])}return n._iq[e]} ;e.amplitude=n})(window,document);amplitude.getInstance().init(apiKey);}")
(str "(function(e,t){var n=e.amplitude||{_q:[],_iq:{}};var r=t.createElement('script');r.type='text/javascript';r.async=true;r.src='https://cdn.amplitude.com/libs/amplitude-4.1.0-min.gz.js';r.onload=function(){if(e.amplitude.runQueuedFunctions){e.amplitude.runQueuedFunctions()}else{console.log('[Amplitude] Error: could not load SDK')}};var i=t.getElementsByTagName('script')[0];i.parentNode.insertBefore(r,i);function s(e,t){e.prototype[t]=function(){this._q.push([t].concat(Array.prototype.slice.call(arguments,0)));return this}}var o=function(){this._q=[];return this};var a=['add','append','clearAll','prepend','set','setOnce','unset'];for(var u=0;u<a.length;u++){s(o,a[u])}n.Identify=o;var c=function(){this._q=[];return this};var l=['setProductId','setQuantity','setPrice','setRevenueType','setEventProperties'];for(var p=0;p<l.length;p++){s(c,l[p])}n.Revenue=c;var d=['init','logEvent','logRevenue','setUserId','setUserProperties','setOptOut','setVersionName','setDomain','setDeviceId','setGlobalUserProperties','identify','clearUserProperties','setGroup','logRevenueV2','regenerateDeviceId','logEventWithTimestamp','logEventWithGroups','setSessionId'];function v(e){function t(t){e[t]=function(){ e._q.push([t].concat(Array.prototype.slice.call(arguments,0)))}}for(var n=0;n<d.length;n++){t(d[n])}}v(n);n.getInstance=function(e){ e=(!e||e.length===0?'$default_instance':e).toLowerCase();if(!n._iq.hasOwnProperty(e)){n._iq[e]={_q:[]};v(n._iq[e])}return n._iq[e]} ;e.amplitude=n})(window,document);amplitude.getInstance().init('" amplitude-api-key "');"))
(defn do-not-track
"Returns true if Do Not Track is enabled"
@ -15,6 +15,5 @@
"Injects the Amplitude bootstrapping script if DNT is disabled"
[]
(when (not (do-not-track))
(let [script (SafeScript/fromConstantAndArgs
(Const/from amplitude-init-code)
amplitude-api-key)])))
(dom/appendChild (.-body (dom/getDocument))
(dom/createDom "script" (clj->js {}) amplitude-init-code))))

View File

@ -5,7 +5,8 @@
[looped-in.hackernews :as hn]
[looped-in.components :as components]
[looped-in.promises :refer [promise->channel]]
[looped-in.logging :as log]))
[looped-in.logging :as log]
[looped-in.analytics :as analytics]))
(enable-console-print!)
@ -233,6 +234,7 @@
(defn init
"Initializes the sidebar"
[]
(analytics/init-amplitude)
(let [initial-state (update-state {:type :loading :loading true} (model))]
(run-render-loop initial-state)
(go (-> (fetch-hits)