Skip to content

Commit

Permalink
Merge pull request smartcontractkit#3247 from smartcontractkit/featur…
Browse files Browse the repository at this point in the history
…e/173928064-Explorer-get-the-Google-Anaytics-ID-from-the-backend
  • Loading branch information
rupurt authored Aug 24, 2020
2 parents a563ae2 + 2b5a723 commit 36f3b2a
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 13 deletions.
2 changes: 1 addition & 1 deletion explorer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Observe network requests using the api having a different origin than the client
Set Google Analytics tracking ID:

```
export REACT_APP_EXPLORER_GA_ID="UA-128878871-10"
GA_ID="UA-128878871-10"
```

## Typescript
Expand Down
3 changes: 3 additions & 0 deletions explorer/client/public/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"GA_ID": ""
}
5 changes: 0 additions & 5 deletions explorer/client/src/__tests__/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,4 @@ describe('config', () => {
process.env.REACT_APP_EXPLORER_BASEURL = 'baseUrl'
expect(Config.baseUrl()).toEqual('baseUrl')
})

it('returns gaId key from the process env', () => {
process.env.REACT_APP_EXPLORER_GA_ID = 'gaId'
expect(Config.gaId()).toEqual('gaId')
})
})
4 changes: 0 additions & 4 deletions explorer/client/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,4 @@ export class Config {
static baseUrl(env = process.env): string | undefined {
return env.REACT_APP_EXPLORER_BASEURL
}

static gaId(env = process.env): string {
return env.REACT_APP_EXPLORER_GA_ID ?? ''
}
}
13 changes: 10 additions & 3 deletions explorer/client/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@ import './index.css'
import * as serviceWorker from './serviceWorker'
import { theme } from '@chainlink/styleguide'
import ReactGA from 'react-ga'
import { Config } from './config'

ReactGA.initialize(Config.gaId())
ReactGA.pageview(window.location.pathname + window.location.search)
interface BeConfig {
GA_ID: string
}

fetch('./config.json')
.then(response => response.json())
.then((beConfig: BeConfig) => {
ReactGA.initialize(beConfig.GA_ID)
ReactGA.pageview(window.location.pathname + window.location.search)
})

JavascriptTimeAgo.locale(en)
moment.defaultFormat = 'YYYY-MM-DD h:mm:ss A'
Expand Down
22 changes: 22 additions & 0 deletions explorer/src/clientConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import fs from 'fs'
import path from 'path'

const workingDir = process.cwd()
const clientDir = 'client/build/config.json'
const fileToUpdate = path.resolve(workingDir, clientDir)
const GA_ID = 'GA_ID'

export function updateClientGaId(id: string) {
fs.readFile(fileToUpdate, 'utf8', (err, file) => {
if (err) {
return console.log(err)
}

const config = JSON.parse(file)
config[GA_ID] = id

fs.writeFile(fileToUpdate, JSON.stringify(config), 'utf8', err => {
if (err) return console.log(err)
})
})
}
4 changes: 4 additions & 0 deletions explorer/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ export class Config {
[key]: value,
})
}

static gaId(env = process.env): string {
return env.GA_ID ?? ''
}
}

/**
Expand Down
2 changes: 2 additions & 0 deletions explorer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { logger } from './logging'
import server from './server'
import { getVersion } from './utils/version'
import { Config } from './config'
import { updateClientGaId } from './clientConfig'

async function main() {
const version = await getVersion(Config.env())
logger.info(version)
updateClientGaId(Config.gaId())

const db = await openDbConnection()
try {
Expand Down

0 comments on commit 36f3b2a

Please sign in to comment.