DEV Community

Kamban
Kamban

Posted on • Originally published at kambanthemaker.com on

Making Google Analytics work from your browser plugin

Google Analytics may not work as it is if you want to get it working from your browser plugin due to security limitations and protocol conflict. To resolve it, follow the steps explained below,

Manifest

First step is to get your manifest file allow GA domain.

"content_security_policy": "script-src 'self' https://www.google-analytics.com; object-src 'self'"

Installing GA

(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-YOUR_ID-1', 'DOMAIN_NAME');ga('set', 'checkProtocolTask', null);ga('send', 'pageview', 'user');

ga('set', 'checkProtocolTask', null); line is important as it makes GA ignore the protocol check. This was causing me a lot of trouble. See here Google Analytics

DOMAIN_NAME is also important, as GA can't detect that for your plugin. Set that to your domain name you want to track!

Debugging

GA has a hidden gem, instead of https://www.google-analytics.com/analytics.js just use https://www.google-analytics.com/analytics_debug.js which will print useful debug messages on the chrome console!

GA

Refer Debugging

Top comments (0)