What I know is recommended is to sent the tracking message to the background script and then call segment's api from the background but it's not working for me.
My background.js code:
!function(){var segmentanalytics=window.segmentanalytics=window.segmentanalytics||[];if(!segmentanalytics.initialize)if(segmentanalytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");else{segmentanalytics.invoked=!0;segmentanalytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","debug","page","once","off","on","addSourceMiddleware","addIntegrationMiddleware","setAnonymousId","addDestinationMiddleware"];segmentanalytics.factory=function(e){return function(){var t=Array.prototype.slice.call(arguments);t.unshift(e);segmentanalytics.push(t);return segmentanalytics}};for(var e=0;e<segmentanalytics.methods.length;e++){var key=segmentanalytics.methods[e];segmentanalytics[key]=segmentanalytics.factory(key)}segmentanalytics.load=function(key,e){var t=document.createElement("script");t.type="text/javascript";t.async=!0;t.src="https://cdn.segment.com/analytics.js/v1/" + key + "/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(t,n);segmentanalytics._loadOptions=e};segmentanalytics._writeKey="EV48CNVRhohYyCczdKXCpW054YNXijDU";segmentanalytics.SNIPPET_VERSION="4.13.2";
segmentanalytics.load("mycode");
}}();
chrome.runtime.onMessage.addListener(function(request, sender) {
if (request.type == "analytics"){
segmentanalytics.track(request.message_name, request.data)
}
});
I keep getting this error:
background.js:41 Refused to load the script 'https://cdn.segment.com/analytics.js/v1/mycode/analytics.min.js' because it violates the following Content Security Policy directive: "script-src 'self' 'unsafe-eval'". Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.
What I tried but didn't work:
- Tried Adding
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self';"to the manifest file