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
- To use in content scripts, you will also need to include it in the background page.
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 scriptsTo 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:
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