Skip to content

Commit

Permalink
Add initial support for Google Analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed Jul 3, 2020
1 parent dd91f98 commit 1cc03fd
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions conf/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type configOptions struct {
CoverArtPriority string
CoverJpegQuality int
UIWelcomeMessage string
GATrackingID string

// DevFlags. These are used to enable/disable debugging and incomplete features
DevLogSourceLine bool
Expand Down Expand Up @@ -81,6 +82,7 @@ func init() {
viper.SetDefault("coverartpriority", "embedded, cover.*, folder.*, front.*")
viper.SetDefault("coverjpegquality", 75)
viper.SetDefault("uiwelcomemessage", "")
viper.SetDefault("gatrackingid", "")

// DevFlags. These are used to enable/disable debugging and incomplete features
viper.SetDefault("devlogsourceline", false)
Expand Down
1 change: 1 addition & 0 deletions server/app/serve_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func ServeIndex(ds model.DataStore, fs http.FileSystem) http.HandlerFunc {
"loginBackgroundURL": policy.Sanitize(conf.Server.UILoginBackgroundURL),
"welcomeMessage": policy.Sanitize(conf.Server.UIWelcomeMessage),
"enableTranscodingConfig": conf.Server.EnableTranscodingConfig,
"gaTrackingId": conf.Server.GATrackingID,
}
j, err := json.Marshal(appConfig)
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions server/app/serve_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ var _ = Describe("ServeIndex", func() {
Expect(config).To(HaveKeyWithValue("enableTranscodingConfig", true))
})

It("sets the gaTrackingId", func() {
conf.Server.GATrackingID = "UA-12345"
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()

ServeIndex(ds, fs)(w, r)

config := extractAppConfig(w.Body.String())
Expect(config).To(HaveKeyWithValue("gaTrackingId", "UA-12345"))
})

It("sets the version", func() {
r := httptest.NewRequest("GET", "/index.html", nil)
w := httptest.NewRecorder()
Expand Down
5 changes: 5 additions & 0 deletions ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"react-admin": "^3.6.1",
"react-dom": "^16.13.1",
"react-drag-listview": "^0.1.7",
"react-ga": "^3.0.0",
"react-jinke-music-player": "^4.13.1",
"react-measure": "^2.3.0",
"react-redux": "^7.2.0",
Expand Down
10 changes: 10 additions & 0 deletions ui/src/App.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react'
import ReactGA from 'react-ga'
import { Provider } from 'react-redux'
import { createHashHistory } from 'history'
import { Admin, Resource } from 'react-admin'
Expand All @@ -19,9 +20,18 @@ import themeReducer from './personal/themeReducer'
import { addToPlaylistDialogReducer } from './dialogs/dialogState'
import createAdminStore from './store/createAdminStore'
import { i18nProvider } from './i18n'
import config from './config'

const history = createHashHistory()

if (config.gaTrackingId) {
ReactGA.initialize(config.gaTrackingId)
history.listen((location) => {
ReactGA.pageview(location.pathname)
})
ReactGA.pageview(window.location.pathname)
}

const App = () => (
<Provider
store={createAdminStore({
Expand Down
1 change: 1 addition & 0 deletions ui/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const defaultConfig = {
loginBackgroundURL: 'https://source.unsplash.com/random/1600x900?music',
enableTranscodingConfig: true,
welcomeMessage: '',
gaTrackingId: '',
}

let config
Expand Down

0 comments on commit 1cc03fd

Please sign in to comment.