I would like to know how many people have done a particular operation. When that operation is performed, you can call Google Analytics directly from the JavaScript program you wrote and record the event.
This function can be used in many elaborate ways, but if you simply want to count the number of times, this is all you need. ts
window.gtag("event", "harvest");
window.detailed account
<script async src="https://www.googletagmanager.com/gtag/js?id=G-GBX75RXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'G-GBX75RXXXX');
</script>
Manual Description
gtag('event', <action>, {
'event_category': <category>,
'event_label': <label>,
'value': <value>
});
gtag('event', 'screen_view', {
'app_name': 'myAppName',
'screen_name': 'Home'
});
About adding properties
TypeScript will generate an error if the type of gtag is unknown, so declare it as follows index.ts
declare global {
interface Window {
gtag: (
a: string,
b: string,
c?: { event_category?: string; event_label?: string; value?: number }
) => unknown;
}
}
I was confused as to whether I had to set up the event, but it looks like I don't have to.
This page is auto-translated from [/nishio/Google Analyticsで何でもカウント](https://scrapbox.io/nishio/Google Analyticsで何でもカウント) using DeepL. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. I'm very happy to spread my thought to non-Japanese readers.