Published at
Updated at
Reading time
2min
This post is part of my Today I learned series in which I share all my web development learnings.

A PR on the MDN compatibility data repository caught my eye today. The pull request added support information for the ping attribute in Safari. I've never seen this attribute โ€“ let's have a look!

The ping attribute on anchor elements

The anchor element's ping attribute accepts a space-separated list of URLs. When an anchor defines ping URLs and someone clicks it, the browser sends a POST request to these specified URLs. You can use it, if you want to track and analyse how users interact with your site.

<a href="https://www.stefanjudis.com/popular-posts/" 
   ping="https://www.stefanjudis.com/tracking/">
  Read popular posts
</a>

To see it in action open your developer tools and click the link below.๐Ÿ‘‡

Read popular posts

And indeed, when you open the network panel in the developer tools (enable "preserve log" to see the request after the browser navigated to a new URL), you see a request flying to the defined URL after clicking the "ping link".

Request headers including a "PING" request payload and a "ping-to" header

The POST request's payload is the single word PING. The ping-to request header holds the link's destination and additional information such as user-agent. It's fascinating that the content-type is text/ping. ๐Ÿ™ˆ

To summarize, the ping attribute offers a leightweight way to implement "link click tracking". Does this mean that you can use it today?

The browser support of the ping attribute

When you look at the attribute's browser compatibility table on MDN, you see that browser support is not so bad.

MDN Compat Data (source)
Browser support info for ping attribute
chromechrome_androidedgefirefoxfirefox_androidsafarisafari_iossamsunginternet_androidwebview_android
12121711661.5โ‰ค37

Chromium browsers (Chrome, Edge, etc.) support it. Firefox's support is behind a browser feature flag (browser.send_pings) and Safari as all green.

Why's no one using ping?

I'm a big fan of native HTML solutions. The question is why no one uses the ping attribute (or do you know a site that uses it?)?

I can only speculate, but one reason could be that user analytics are mainly driven by 3rd party providers such as Google Analytics.

To use these you have to embed a single script into your site. The JavaScript will track all the user behaviour, and there's no infrastructure to set up. It just works.

If you base your tracking on the ping attribute, you have to adjust all the links on your site. This process includes more maintenance and development work. That's a strong argument against using the ping attribute.

Nevertheless, it's good to know that it exists. If you use ping, I'd love to learn more about what you do with it!

Was this TIL post helpful?
Yes? Cool! You might want to check out Web Weekly for more quick learnings. The last edition went out 10 days ago.
Stefan standing in the park in front of a green background

About Stefan Judis

Frontend nerd with over ten years of experience, freelance dev, "Today I Learned" blogger, conference speaker, and Open Source maintainer.

Related Topics

Related Articles