I need to implement a system (www . mydomain .com) where users can register to get a custom JS snippet and embed it on their website (www . userdomain .com). This snippet displays a widget on their site (www . userdomain .com) and performs some other actions as well. I need to collect page views which users can view by signing-in to their dashboard on mydomain .com, so I figure there are 2 ways:
using an image pixel element which has its src attribute set to mytrackingscript.php. This script will receive a request when the image element loads and thus I can store the metrics in DB. Something like this:
Instead of using image pixel, I make an XHR request to mytrackingScript.php and save the metrics to DB.
I understand there would CORS restrictions coming in place with the 2nd approach, and I would have to allow CORS for my site.
My questions:
Is the first approach preferable to the second approach
If I enable CORS, are there security implications, given that I would be doing so(enabling CORS) only for that script and it would be just writing the metrics to DB and not interacting with any critical data?
I realize that either of these approaches can be used for tracking page views, but if I need to collect more data which requires periodically connecting to the DB (eg click events and other user interactions with the widget), can this be done using the first approach? I am thinking along the lines of reloading the image pixel src at regular intervals would server the purpose by connecting to the script periodically? Are my assumptions correct?
And very importantly, do I need to even write custom code for this? Can I use Google Analytics tracking pixel for this? The flow I want is: 1. users A and B register on mydomain, and get a JS snippet and embed on their sites userdomainA and userdomainB 2. snippet displays a widget on their sites and tracks the activity on the sites 3. A and B can login to display their dashboards on mydomain.com/dashbaord and see the analytics for just their domains i.e userdomainA and userdomainB respectively
Is it possible to achieve this use Google Analytics? I have not used Google Analytics much so I thought I'd ask
Thanks.