-
-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use measurement protocol to get at least some statistics of usage.
- Loading branch information
Showing
15 changed files
with
218 additions
and
49 deletions.
There are no files selected for viewing
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
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
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,87 @@ | ||
import { stringify } from 'querystring'; | ||
import { getMachineId } from './services/settings.api'; | ||
|
||
const trackingId: string = process.env.REACT_APP_TRACKING_ID || ''; | ||
const secretKey: string = process.env.REACT_APP_SECRET_KEY || ''; | ||
|
||
const config = { | ||
measurementId: trackingId, | ||
measurementSecret: secretKey, | ||
|
||
measurementUrl: 'https://www.google-analytics.com/mp/collect', | ||
measurementUrlDebug: 'https://www.google-analytics.com/debug/mp/collect', | ||
clientId: null, | ||
}; | ||
|
||
const getUrl = () => | ||
new URL( | ||
`${config.measurementUrl}?${stringify({ | ||
measurement_id: config.measurementId, | ||
api_secret: config.measurementSecret, | ||
})}`, | ||
); | ||
|
||
/** | ||
* Tracks an analytics event via measurement protocol, | ||
* i.e. via API. | ||
* | ||
* @param name the event name. | ||
* | ||
* @param params the event parameters. | ||
* | ||
* @param clientId the clientId for this event. Generated if not supplied. | ||
*/ | ||
export const sendEvent = async ({ name, params }: { name: string; params }) => { | ||
const url = getUrl(); | ||
|
||
if (!config.clientId) { | ||
const machineId = await getMachineId(); | ||
config.clientId = machineId; | ||
} | ||
|
||
return fetch(url, { | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
client_id: config.clientId, | ||
non_personalized_ads: true, | ||
|
||
events: [ | ||
{ | ||
name, | ||
params, | ||
}, | ||
], | ||
}), | ||
}); | ||
}; | ||
|
||
export const setUserProperty = async (name, value) => { | ||
const url = getUrl(); | ||
|
||
if (!config.clientId) { | ||
const machineId = await getMachineId(); | ||
config.clientId = machineId; | ||
} | ||
|
||
return fetch(url, { | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
client_id: config.clientId, | ||
user_properties: { | ||
[name]: { | ||
value, | ||
}, | ||
}, | ||
}), | ||
}); | ||
}; | ||
|
||
export const pageView = ({ location, title }) => { | ||
return sendEvent({ | ||
name: 'page_view', | ||
params: { | ||
page_title: title, | ||
page_location: location, | ||
}, | ||
}); | ||
}; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
import { useEffect } from 'react'; | ||
import { useLocation } from 'react-router-dom'; | ||
import ReactGA from 'react-ga4'; | ||
|
||
import { sendPageEvent } from './useGoogleAnalytics.utils'; | ||
|
||
export function useGoogleAnalytics() { | ||
const location = useLocation(); | ||
|
||
useEffect(() => { | ||
console.info('Setting page', location.pathname); | ||
ReactGA.set({ path: location.pathname, search: location.search }); // Update the user's current page | ||
ReactGA.send({ hitType: 'pageview', page: location.pathname }); | ||
|
||
sendPageEvent(location.pathname, location.search); | ||
}, [location]); | ||
} |
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,14 @@ | ||
import { pageView, sendEvent, setUserProperty } from './ga-measurement-protocol'; | ||
|
||
export function setAppParams() { | ||
setUserProperty('app_version', process.env.REACT_APP_VERSION || '1.0.0'); | ||
} | ||
|
||
export function sendPageEvent(path, search) { | ||
setAppParams(); | ||
pageView({ location: path, title: path }); | ||
} | ||
|
||
export function sendOpenTrayEvent() { | ||
sendEvent({ name: 'open_tray', params: {} }); | ||
} |
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 |
---|---|---|
|
@@ -3581,11 +3581,16 @@ | |
resolved "https://registry.npmjs.org/@types/mime/-/mime-3.0.1.tgz" | ||
integrity sha512-Y4XFY5VJAuw0FgAqPNd6NNoV44jbq9Bz2L7Rh/J6jLTiHBSBJa9fxqQIvkIld4GsoDOcCbvzOUAbLPsSKKg+uA== | ||
|
||
"@types/node@*", "@types/node@>=12.12.47", "@types/node@>=13.7.0", "@types/node@^18.11.9": | ||
"@types/node@*", "@types/node@>=12.12.47", "@types/node@>=13.7.0": | ||
version "18.11.9" | ||
resolved "https://registry.npmjs.org/@types/node/-/node-18.11.9.tgz" | ||
integrity sha512-CRpX21/kGdzjOpFsZSkcrXMGIBWMGNIHXXBVFSH+ggkftxg+XYP20TESbh+zFvFj3EQOl5byk0HTRn1IL6hbqg== | ||
|
||
"@types/node@^20.4.2": | ||
version "20.4.2" | ||
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.4.2.tgz#129cc9ae69f93824f92fac653eebfb4812ab4af9" | ||
integrity sha512-Dd0BYtWgnWJKwO1jkmTrzofjK2QXXcai0dmtzvIBhcA+RsG5h8R3xlyta0kGOZRNfL9GuRtb1knmPEhQrePCEw== | ||
|
||
"@types/parse-json@^4.0.0": | ||
version "4.0.0" | ||
resolved "https://registry.npmjs.org/@types/parse-json/-/parse-json-4.0.0.tgz" | ||
|
@@ -9346,6 +9351,11 @@ [email protected]: | |
dependencies: | ||
side-channel "^1.0.4" | ||
|
||
querystring@^0.2.1: | ||
version "0.2.1" | ||
resolved "https://registry.yarnpkg.com/querystring/-/querystring-0.2.1.tgz#40d77615bb09d16902a85c3e38aa8b5ed761c2dd" | ||
integrity sha512-wkvS7mL/JMugcup3/rMitHmd9ecIGd2lhFhK9N3UUQ450h66d1r3Y9nvXzQAW1Lq+wyx61k/1pfKS5KuKiyEbg== | ||
|
||
querystringify@^2.1.1: | ||
version "2.2.0" | ||
resolved "https://registry.npmjs.org/querystringify/-/querystringify-2.2.0.tgz" | ||
|
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
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
Oops, something went wrong.