Include everywhere in your chrome extension to patch an analytics api.

npm install analytics-include
4 downloads in the last week
6 downloads in the last month

KissMetrics in Chrome Extensions wi/kiss-include.js

To use KissMetrics in Chrome Extensions, we made a script that magically patches the JavaScript api for use everywhere within Chrome Extensions. You simply include it in your background page and content scripts, and call _kmq.push like normal.

Download the patch script right from this repository, or do npm install kiss-include

Important points

  • To use in content scripts, you will also need to include it in the background page.


  • Asynchronously (recommended)

    Setup this code, and being push'ing to _kmq:

      if (typeof _kmq === 'undefined') {
        window._kmq = [];
      window._kmk = 'c168ad9f6287ggbcfe92a883fc3c8c0f904e7972';

    At some later point, be sure to include kiss-include.js. It will detect your _kmk id, and set itself up.

  • Directly

    Include kiss-include.js before this code in your extension, and then call kissInclude with your _kmk account id:

  • Setting a domain

    As an extension, you might want to manually specify a domain like so:

      window.KM_COOKIE_DOMAIN = '';

    This code needs to be placed before the library is initialized.

Extra details on the script

Wherever you include the script, it detects it's environment and responds accordingly. This script alleviates the headache associated with chrome's isolated world's - you just use the JS API as defined in the docs.

Including in content scripts

To work in content scripts, you need to make sure it's also included in the background page

  //in you manifest.json:
  "content_scripts": [
        "js": [ "YourScripts.js", "kiss-include.js" ],
        "matches": [ "<all_urls>" ],
        "run_at": "document_start"
Including in background page:
  //in your background.html:
  <script src="kiss-include.js"></script>

If you include kiss-include.js after your code, you need to use asynchronous initialization. If you include it before your code, you'll need to use direct initialization.

npm loves you