Search code examples
javascriptsafariaccess-controluniversal-analytics

Not allowed by Access-Control-Allow-Origin using universal-analytics on Safari


Can't seem to figure this out.

I'm using the universal-analytics nom package (https://www.npmjs.com/package/universal-analytics) in my react.js app and it works great for recording page views on FireFox and Chrome to my Google Analytics account. But on Safari I get the following error:

Failed to load resource: Origin http://thematerik.com is not allowed by Access-Control-Allow-Origin. http://www.google-analytics.com/collect?dp=%2F&v=1&tid=UA-XXXXX-XX&cid=0a2a3a86-2646-41f7-93fd-1e0bc30eaacd&t=pageview
// Note that I do have a proper Google Analytics id, just replaced it with UA-XXXXX-XX here.

I know what Access-Control-Allow-Origin means but don't know how to resolve it in this context as it's universal-analytics doing the requests. Thanks for the help.


Solution

  • Solved this by initialising my visitor object by setting the content-type. Something Safari doesn't seem to do on its own:

    var ua = require('universal-analytics');
    var accountId = 'UA-XXXXX-XX';
    var visitor = ua(accountId, {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
    });