Making Google Analytics work from your browser plugin

— 3 minute read

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 permalink

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

"content_security_policy": "script-src 'self'; object-src 'self'"

Installing GA permalink

(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

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 permalink

GA has a hidden gem, instead of just use which will print useful debug messages on the chrome console!

Refer Debugging