Include global JS script with env/config variables

Hey all, I'm trying to create an addon that adds a JS snippet to every page - (actually I'd need it only on collection editing page) - and that can read from an env variable or config value.

For example, imagine including the Google Analytics tracking code. You'll need something like this:

(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');

In the second-last LoC a token is required (UA-XXXXX-Y) and you would place that inside an env variable (and a config value), but how can you read that value from a JS file?

I've read that you could add it as a MIX variable, but that would require the user installing the Addon to rebuild the assets both in local and production.

Thank you!

>>>>>>> Unanswered <<<<<<<
0 Replies
2 Followers