Trouble recreating Google Analytics (UA) events in GA4

Viewed 103

I have a bunch of mp3s and an audio player on my site. In GA3 (UA) I had an event that fired every time I hit the play button on a specific track. The event name was jPlayer, the track in question is the variable 'mediaName', which is the name of the mp3 file which is derived when I click play. In my code, mediaName is a string that gets sent out to GA. i.e. mediaName might be 'mycooltrack.mp3', or 'anothergoodtrack.mp3'

 ga ('send', 'event', 'jPlayer', mediaName); 

After reading the GA4 docs, I guessed that I needed to make a custom dimension, called "Track" which took the User Property/Parameter: mediaName.

So the new code in my function looked like:

 gtag('event', 'jPlayer', {
           'Track': mediaName
           });

But after several days this hasn't been working either.

I guess I don't really understand what dimensions are and how to send an event with an 'on-the-fly' name variable to GA4, like I've been doing with UA for years.

1 Answers

You need to first create the event in GA4 interface. https://support.google.com/analytics/answer/10085872?hl=en&ref_topic=9756175#zippy=%2Cin-this-article%2Creserved-prefixes-and-event-names%2Cweb

It is under "Configure" on the left-hand side. Then go through the steps. enter image description here

Your configuration will look something like this, if you really like the "jPlayer" event name, then just replace "audio_play" with "jPlayer" enter image description here

The code you used is still relevant and can stay the same, I'd just write "track" in lowercase.

Related