0
votes

In my project, I use a few RequireJS modules. I have used the Require JS optimizer (r.js)to package them further into various modules. So, if we have require modules like a.js, b.js, c.js and d.js after packaging we get this :

abc.js ( which is a combination of a.js and b.js)

a.js, b.js

def.js ( which is a combination of c.js and d.js)

c.js, d.js

We have made these modules abc.js and def.js available over CDN and end users use it. Is there a way to find out who is using a.js and who is using b.js? I mean, can we detect who downloaded which module in their application. This becomes a little tricky because sometimes it will be loaded from browser cache also. At least can we do it at the level of abc.js and def.js?

1

1 Answers

1
votes

The problem is if you're distributing both modules in the same file then you need some way to track the usage of each module (right?) and to complicate thing further, browsers cache static files, other users could download your modules and self host, etc etc.

I think this is a slightly offside solution but it should work. You could grab a JavaScript analytics library like Segment.IO:

https://segment.com

and in your definition for module A (inside it's "factory" or definition block) you could track it's usage:

analytics.track('module A was required');

likewise in module B's definition:

analytics.track('module B was required');

and that way even though they're defined in the same file and could be cached, only when the user requires the individual module do you fire the tracking call, so you'll know exactly when a certain module of yours is used.