I've built Javascript widgets that have been installed on other websites.

How can I use google analytics on my widgets like I would use analytics on a web page to track the traffic?

FYI, the Javascript code for my widgets is created from PHP pages.


1 Answer 1


It would likely be extremely unethical (and potentially illegal) to include tracking snippets in JS widgets that get installed on client sites. Unless they're iframe widgets, but I don't see anything about iframes in your post.

However, technically, there's no technical difference between implementing basic GA tracking on a site versus a JS widget. You just load the GA library, initialize it and send events. Any basic tracking manual would do.

  • Thanks for your answer. However, I don't see what ethics has to do with my request. I just want to track who uses my widgets.
    – jennie788
    Nov 22, 2021 at 3:53
  • 1
    Forget about ethics, in many countries (e.g. all of the EU) this would not even be legal without permission by both the website owner and the individuals being tracked. Nov 22, 2021 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.