Chrome Extensions, meet KissMetrics. KM, meet .crx
JavaScript
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.gitattributes
.gitignore
README.markdown
kiss-include.js
package.json

README.markdown

KissMetrics in Chrome Extensions w/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 devinrhode2/kiss-include

Important points

Initialize

  • 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:

    kissInclude('ioheiorfhaksjnaoiewhoasdhjf');
  • Setting a domain

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

    window.KM_COOKIE_DOMAIN = 'www.mydomain.com';

    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

```javascript //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.

This also includes a convenience event method on _kmq.push. Instead of _kmq.push(['record', 'something']); use: event('record', 'something');, but if you want to just record something, one argument defaults to 'record' so you can go: event('something');

Add as npm dependency

Plain npm install and npm update won't work with kiss-include unless you add "kiss-include": "git://github.com/devinrhode2/kiss-include.git" to "dependencies" in your package.json. You can always directly do npm install devinrhode2/kiss-include and npm update devinrhode2/kiss-include

License

(The MIT License)

Copyright © 2012 Devin Rhode <DevinRhode2@Gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.