Track audio plays in Google Analytics 4 on WordPress
Published Updated 4 min read
Run an audio site without knowing what people listen to and you are publishing in the dark. WordPress plays the audio and GA4 collects the data, but the two never speak. GA4 records the pageview reliably; it does not record whether anyone pressed play, how far they got, or whether they stayed to the end.
MonsterInsights Pro includes media tracking as part of a full analytics suite. If you already use it for ecommerce reports, form conversions or scroll depth, audio tracking comes along at no extra cost. If GA4 went in through Site Kit and you only need to know when someone presses play, you would be paying for a large plugin to add one JavaScript listener.
This plugin adds a field for your GA4 Measurement ID and a script that attaches to every HTML5 audio element on the site. When a visitor plays, pauses or finishes a track, a GA4 event fires with the audio's title and source URL. That is the complete scope.
What GA4 tracks by default and what it misses
GA4's enhanced measurement covers pageviews, scrolls, outbound clicks, site search, form interactions and video engagement. The video events are for embedded YouTube players and do not apply to HTML5 audio served from your own server or a CDN.
For an audio drama site with thirty episodes, the pageview says someone opened the episode page. It says nothing about whether they pressed play, listened for two minutes or reached the end. Those answers live on the audio element, and GA4 does not look there without custom events.
The gap is small: event listeners on audio elements that call gtag when they fire. It does not justify a large analytics plugin unless you would use the rest of it anyway.
How the events reach GA4
On each page load, a small script finds every audio element in the page (the audio block renders one, the playlist block several) and attaches listeners once the page is ready. Any player that outputs a plain HTML5 audio tag works. Podcast plugins such as Seriously Simple Podcasting and PowerPress use custom players that may not, so check the page source for an audio element first.
Play fires audio_play with audio_title (the track's filename) and audio_url (its full source). Pause fires audio_pause, and the ended event fires audio_complete. They show immediately in the Realtime view and under Reports > Engagement > Events within about 24 hours of the first play.
Settings > Audio Analytics takes the Measurement ID, in the form G-XXXXXXXXXX, from the Data Streams page of your GA4 property. The plugin loads the Google tag itself, so Site Kit or another analytics plugin is not needed. The visible page does not change: two script tags in the footer and listeners on the audio elements.
Reading the data in GA4
Once fired, the events appear in the Events table. To see which episodes draw the most plays, register audio_title as an event-scoped custom dimension under Admin > Data Display > Custom Dimensions. After up to 24 hours you can filter audio_play by title for play counts per episode.
To count finished episodes as conversions, find audio_complete in the Events list and switch on Mark as conversion; it then appears in the Conversions report with your other conversion events.
Plays and completes for the same title give a completion rate with no further setup. 400 plays and 180 completes means roughly 45 per cent of listeners reach the end.
When MonsterInsights is the better answer
If you already use MonsterInsights, its media tracking covers audio and video with no separate plugin, and its Media report shows play counts, percentage played and comparisons across tracks without a line of code.
MonsterInsights Pro also reports 25, 50, 75 and 100 per cent milestones for each track, which this build does not. If listener retention curves shape how you plan content, that is the reason to use it. A follow-up message can add milestones here too, by watching currentTime against duration on a timeupdate listener and firing each threshold once per session.
The case for building your own is practical: if you do not already pay for MonsterInsights and only need plays and completions, a full suite's annual cost does not match the size of the problem.
What you get
Included
- Fires audio_play with the track title and URL when playback starts on any HTML5 audio element
- Fires audio_pause when a visitor pauses and audio_complete when the track reaches the end
- Works with WordPress's built-in audio block and playlist block
- Settings screen under Settings > Audio Analytics for the GA4 Measurement ID
- Loads the Google tag directly: no Site Kit or other analytics plugin required
Not included
- Playback milestone events at 25, 50, or 75 per cent completion
- Audio players rendered by podcast plugins that do not use a plain HTML5 audio element
- Per-listener or logged-in user attribution
- Video tracking of any kind
- Playlist navigation events when a visitor skips to a different track mid-session
MonsterInsights does the things on the right. This build covers the part most sites use.
Questions
GA4 is already installed through Site Kit. Will entering my Measurement ID here cause a conflict?
Entering the same ID in both sends GA4 a double pageview on every page load. Either leave the field here blank, in which case the audio events still fire through the gtag function Site Kit already loads, or enter it here and switch off Site Kit's GA4 snippet. Check the Realtime report after any change.
No events are appearing in GA4. What should I check?
That the Measurement ID matches your GA4 data stream exactly; that you are testing in the Realtime report, since the standard reports lag 24 to 48 hours; and the browser console for JavaScript errors when you press play. An ad blocker can also stop gtag calls.
Build it in Steem
The button opens the builder with this guide's prompt filled in, so you can change anything before it runs. Nothing is built, and nothing is charged, until you send it.
Read the prompt (301 words)
Build a WordPress plugin called Simple Audio Analytics. Plugin Name: Simple Audio Analytics. Description: Fires GA4 events when visitors play, pause, or finish HTML5 audio elements on WordPress pages. Single PHP file. Register a settings group using register_setting with option group audio_analytics and option name audio_analytics_settings. Add a settings page using add_options_page with page title Audio Analytics, menu title Audio Analytics, capability manage_options, and slug audio-analytics. In the settings page callback, output a form with method post posting to options.php. Call settings_fields with audio_analytics. Output a table with one row: a label for audio_analytics_settings[measurement_id], a text input with name audio_analytics_settings[measurement_id] and value set to esc_attr of the current option, and a description paragraph reading Your GA4 Measurement ID in the format G-XXXXXXXXXX. Add a submit button using get_submit_button. Register a settings section with id audio_analytics_main on page audio-analytics. Register a settings field for measurement_id on that page and section. Hook into wp_footer at priority 20. In the callback, get measurement_id from get_option('audio_analytics_settings')['measurement_id'] and pass it through esc_js. If it is empty, return early. Output a script tag with async attribute and src set to https://www.googletagmanager.com/gtag/js?id= followed by the measurement ID. Then output an inline script block that: declares window.dataLayer as window.dataLayer or an empty array, declares the gtag function pushing arguments to dataLayer, calls gtag js with new Date(), calls gtag config with the measurement ID and {send_page_view: false}. After the config call, add a DOMContentLoaded event listener that: calls document.querySelectorAll('audio'), loops the resulting elements, and for each adds three event listeners. On play: call gtag event audio_play with parameters audio_title set to element.title if truthy or else the last segment of element.currentSrc split on slash, and audio_url set to element.currentSrc. On pause: call gtag event audio_pause with the same parameters. On ended: call gtag event audio_complete with the same parameters.
Steem