-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#317: Using hooks instead of components
- Loading branch information
Showing
10 changed files
with
79 additions
and
120 deletions.
There are no files selected for viewing
20 changes: 0 additions & 20 deletions
20
client/src/google-analytics/__tests__/__snapshots__/google-analytics.test.jsx.snap
This file was deleted.
Oops, something went wrong.
30 changes: 0 additions & 30 deletions
30
client/src/google-analytics/__tests__/google-analytics.test.jsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
describe('Google Analytics hooks test suite', () => { | ||
|
||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {useEffect} from 'react'; | ||
import get from 'lodash/get'; | ||
import isFunction from 'lodash/isFunction'; | ||
import {googleAnalyticsTrackingId} from './selectors'; | ||
|
||
const updateLocation = trackingId => { | ||
if (isFunction(window.gtag)) { | ||
window.gtag('config', trackingId, { | ||
page_path: get(window, 'location.pathname') | ||
}); | ||
} | ||
}; | ||
|
||
export const useAnalytics = () => { | ||
const trackingId = googleAnalyticsTrackingId(); | ||
if (trackingId) { | ||
useEffect(() => { | ||
const scriptLoader = document.createElement('script'); | ||
scriptLoader.async = true; | ||
scriptLoader.src = `https://www.googletagmanager.com/gtag/js?id=${trackingId}`; | ||
document.body.appendChild(scriptLoader); | ||
window.gtag = function gtag() { | ||
window.dataLayer = window.dataLayer || []; | ||
window.dataLayer.push(arguments); | ||
}; | ||
window.gtag('js', new Date()); | ||
updateLocation(trackingId); | ||
return () => document.body.removeChild(scriptLoader); | ||
}, []); | ||
} | ||
}; | ||
|
||
export const updateAnalytics = () => { | ||
const trackingId = googleAnalyticsTrackingId(); | ||
if (trackingId) { | ||
useEffect(() => { | ||
updateLocation(trackingId); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
|
||
export {default} from './google-analytics'; | ||
export { | ||
useAnalytics, | ||
updateAnalytics | ||
} from './hooks'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 17 additions & 41 deletions
58
client/src/routes/__tests__/__snapshots__/routes.test.jsx.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,21 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Routes component test suite Snapshot render 1`] = ` | ||
<Router | ||
basename="/" | ||
history={ | ||
Object { | ||
"action": "POP", | ||
"block": [Function], | ||
"createHref": [Function], | ||
"go": [Function], | ||
"goBack": [Function], | ||
"goForward": [Function], | ||
"length": 1, | ||
"listen": [Function], | ||
"location": Object { | ||
"hash": "", | ||
"pathname": "/", | ||
"search": "", | ||
"state": undefined, | ||
}, | ||
"push": [Function], | ||
"replace": [Function], | ||
} | ||
} | ||
> | ||
<Switch> | ||
<Route | ||
exact={true} | ||
path="/configuration-not-found" | ||
render={[Function]} | ||
/> | ||
<Connect(ApplicationReadyRoute) | ||
exact={true} | ||
path="/login" | ||
render={[Function]} | ||
/> | ||
<Connect(ApplicationReadyRoute) | ||
component={[Function]} | ||
exact={true} | ||
path="/" | ||
/> | ||
</Switch> | ||
</Router> | ||
<Switch> | ||
<Route | ||
exact={true} | ||
path="/configuration-not-found" | ||
render={[Function]} | ||
/> | ||
<Connect(ApplicationReadyRoute) | ||
exact={true} | ||
path="/login" | ||
render={[Function]} | ||
/> | ||
<Connect(ApplicationReadyRoute) | ||
component={[Function]} | ||
exact={true} | ||
path="/" | ||
/> | ||
</Switch> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters