nicholas
nicholas

Reputation: 14563

Use Google Analytics in Chrome packaged app?

I have an existing webapp that I'd like to package up as a chrome packaged app, but I'm getting the following error when trying to hit google analytics:

Refused to load the image 'http://www.google-analytics.com/__utm.gif...' because it violates the following Content Security Policy directive: "img-src 'self' blob: filesystem: data: chrome-extension-resource:".

Adding content_security_policy to my manifest.son as described here seems to be out of date and is giving me the error.

There were warnings when trying to install this extension: 'content_security_policy' is only allowed for extensions and legacy packaged apps, but this is a packaged app.

What is the package app way to register a google analytics page view?

Upvotes: 1

Views: 800

Answers (1)

woxxom
woxxom

Reputation: 73506

The apps documentation says:

Your Chrome App can only refer to scripts and objects within your app, with the exception of media files (apps can refer to video and audio outside the package). Chrome extensions will let you relax the default Content Security Policy; Chrome Apps won’t.

  • Use chrome-platform-analytics version of the GA script that supports apps
  • Include the GA script in your app package, load it as a local script:

    <script src="google-analytics-bundle.js"></script>
    <script src="mainwindow.js"></script>
    
  • Add the necessary permissions in manifest.json:

    "permissions": ["https://www.google-analytics.com/*"]
    
  • Initialize it:

    service = analytics.getService('my_app');
    service.getConfig().addCallback(function(config) {
        console.log(config.isTrackingPermitted());
        config.setTrackingPermitted(true);
    });
    
    // Get a Tracker using your Google Analytics app Tracking ID.
    tracker = service.getTracker('UA-XXXXX-X');
    
    // Record an "appView" each time the user launches your app or goes to a new
    // screen within the app.
    tracker.sendAppView('MainView');
    

The code is quoted from Google analytics app in the official app samples repository on github.

Upvotes: 1

Related Questions