How to know if a JS file is loaded on server side?

Viewed 294

I'm a developer for the website Friconix, a collection of free icons. Users can include our JS file on their web pages as explain here : https://friconix.com/start/.

I would like to gather statistics on clients using our tools. How to know, on server side, information on pages (URL or at least domains) that request our JS file ?

It's important to explain that, for decreasing the loading time, the JS file is not dynamically generated. There is no PHP file loaded every time the file is requested. The file is saved in plain text on our server. I wonder if the right solution is not to add something in the .htaccess file ?

2 Answers

Since the script is requested from your server every time a user loads a browser-page you can track who and how often that path is requested.

A simple approach is that it will be present in you request log files. So you can create a script and read your log files every so often.

A second approach is to setup a special rule/location in nginx/apache/which-ever-server-you-are-running

A third approach is to serve the script via CDN that has all these attributes built in (ie. CloudFront)

This can be done via a simplistic REST API call from the script. Thus when your script will load it will call the rest API via an AJAX or XHR call. The request can contain a unique client ID. On the server-side, you can implement a simple API that will accept these requests and store them by extracting the necessary information for analytics.

All the information like domains and IP about the client can be gathered from the API request or requests which will be made from clients page.

Reference - How do I call a JavaScript function on page load?

Related