I am trying to create a GA4 (google analytics) custom event with custom params, like this:
{ "event_name": "game_over", "action": "message shown", "label": {the current url}, "data": {some JSON object data} }
Which I will send from my react website to the already setup google analytics project.
What I have tried:
I read the docs of GA4 and has found it unhelpful and in the google searches nothing came but 'Universal Analytics' and no GA4 articles.
Except for 1, the following: custome event GA4, which helped me to create a custom event but not custom params as well.
I am still searching for an answer, but if someone will be able to help it would be great!
(Note for 1500 or higher stack overflow members: Please add the GA4 (or ga4) tag to the question as I can't do it because I don't have permit to do so, and also erase this note after [I think it is needed])
Finally I have found a solution.
You need to do as written in the following article: custome event GA4
But in step 2 change the 'TrackGoogleAnalyticsEvent' method to be as follow:
const TrackGoogleAnalyticsEvent = (
category,
event_name,
label,
data
) => {
console.log("GA event:", category, ":", event_name, ":", label);
let event_params = {
category,
label,
...data
};
// Send GA4 Event
ReactGA4.event(event_name, event_params);
};
And in the client side you need to have the following code:
TrackGoogleAnalyticsEvent(
"game_over",
"message shown",
window.location.pathname + window.location.search,
{ id: 1234, username: "john" }
);
Note: You can enter as many parameters you like in the JSON object.
For more information, a link to the 'react-ga4' package: react-ga4 docs