The `ping` attribute on anchor links

I didn’t know this was a thing until Stefan Judis’s post:

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

You give an anchor link a URL via a ping attribute, and the browser will hit that URL with a web request …


The post The `ping` attribute on anchor links appeared first on CSS-Tricks.

You can support CSS-Tricks by being an MVP Supporter.

I didn’t know this was a thing until Stefan Judis’s post:

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

You give an anchor link a URL via a ping attribute, and the browser will hit that URL with a web request (a literal PING) when clicked. The headers have a ping-to key with the href value of the link.

Why? Data. Wouldn’t it be nice to know what off-site links people are clicking on your website?

Even if you have Google Analytics installed, you don’t get that data by default. You’d have to write something custom or use something like their autotrack plugin with the outboundLinkTracker. Whatever you do, it is non-trivial, as in order to work, it has to:

  1. Have JavaScript intervene
  2. Prevent the default action of the link (going to the website)
  3. Track the event (send a ping somewhere)
  4. Then tell the browser to actually go to the website (window.location = …)

That’s because running a bit of JavaScript to ping your tracking service is unreliable on an off-site click. Your site is unloading and the new site loading as fast as possible, meaning that ping might not get a chance to run.

Presumably, with the ping attribute, you don’t have to do this little JavaScript dance of sending the ping before the page unloads — it will “just work.” That’s a big plus for this technique. It’s so cool to move complex ideas to lower-level languages that work easier and better.

There are heaps of downsides though. You don’t exactly get a nice API for sending along metadata. The best you could do is append a query string for extra data (e.g., was the link in the footer? or was it in a blog post?). But perhaps the biggest limitation is that it’s only for anchor links. If you were building a really serious event tracking thing, you’d want it to be useful for any type of event (not just clicks) on any type of element (not just links). So it’s not terribly surprising this is almost entirely the JavaScript’s domain.

Direct Link to ArticlePermalink


The post The `ping` attribute on anchor links appeared first on CSS-Tricks.

You can support CSS-Tricks by being an MVP Supporter.


Print Share Comment Cite Upload Translate
APA
Chris Coyier | Sciencx (2024-03-28T13:59:22+00:00) » The `ping` attribute on anchor links. Retrieved from https://www.scien.cx/2021/04/08/the-ping-attribute-on-anchor-links/.
MLA
" » The `ping` attribute on anchor links." Chris Coyier | Sciencx - Thursday April 8, 2021, https://www.scien.cx/2021/04/08/the-ping-attribute-on-anchor-links/
HARVARD
Chris Coyier | Sciencx Thursday April 8, 2021 » The `ping` attribute on anchor links., viewed 2024-03-28T13:59:22+00:00,<https://www.scien.cx/2021/04/08/the-ping-attribute-on-anchor-links/>
VANCOUVER
Chris Coyier | Sciencx - » The `ping` attribute on anchor links. [Internet]. [Accessed 2024-03-28T13:59:22+00:00]. Available from: https://www.scien.cx/2021/04/08/the-ping-attribute-on-anchor-links/
CHICAGO
" » The `ping` attribute on anchor links." Chris Coyier | Sciencx - Accessed 2024-03-28T13:59:22+00:00. https://www.scien.cx/2021/04/08/the-ping-attribute-on-anchor-links/
IEEE
" » The `ping` attribute on anchor links." Chris Coyier | Sciencx [Online]. Available: https://www.scien.cx/2021/04/08/the-ping-attribute-on-anchor-links/. [Accessed: 2024-03-28T13:59:22+00:00]
rf:citation
» The `ping` attribute on anchor links | Chris Coyier | Sciencx | https://www.scien.cx/2021/04/08/the-ping-attribute-on-anchor-links/ | 2024-03-28T13:59:22+00:00
https://github.com/addpipe/simple-recorderjs-demo