DEV Community

Discussion on: How to log user activities using the Beacon Web API?

Collapse
 
4nduril profile image
Tobias Barth

Hi, thanks for the article. I am considering sendBeacon for tracking in my current project. One question: everywhere it reads "small amount of data". What does that mean? Is there a limit? A best practice?
Regards

Collapse
 
atapas profile image
Tapas Adhikary

Hi Tobias,

This is a great question. Yes, there is a limit and it is up to the user-agents(browser vendors) to decide what would be the limit.

The spec says,

The user agent must restrict the maximum data size to ensure that beacon requests are able to complete quickly and in a timely manner.

Check out this link for more details: w3.org/TR/beacon/#sec-sendBeacon-m...

For example, with research, I had found that someone tested the limit on an older version of chrome on windows and found the limit to be, (2^16).

Please take a look.

var url = 'http://jsfiddle.net?sendbeacon';
var n = 65536; // sendBeacon limit for Chrome v40 on Windows (2^16)

// this method courtesy of http://stackoverflow.com/questions/14343844/create-a-string-of-variable-length-filled-with-a-repeated-character
var data = new Array(n+1).join('X'); // generate string of length n

if(!navigator.sendBeacon(url, data))
{
   alert('data limit reached');
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
4nduril profile image
Tobias Barth

Oh great, thank you very much. I hoped that there was a spec'd limit but this way I can check the relevant browsers.

Thread Thread
 
atapas profile image
Tapas Adhikary

Welcome, Tobias!

Collapse
 
atapas profile image
Tapas Adhikary

I have also added a small section about it in the article now.