{
 "blyg": "0.4",
 "id": "149vyjwt8swwyf3xxqzb60bvbs",
 "kind": "thread",
 "origin": "https://notionparallax.co.uk/",
 "page": "https://notionparallax.co.uk/2016/trying-to-capture-a-save-to-pocket-event",
 "author": {
  "name": "Ben Doherty",
  "url": "https://notionparallax.co.uk/"
 },
 "created": "2016-05-20T17:08:27Z",
 "updated": "2016-05-20T17:08:27Z",
 "version": 1,
 "content_md": "# Trying to capture a 'Save to Pocket' event\n\n\nI wanted to be able to track how many people save articles to Pocket from this site. I thought that given how much [I use](http://notionparallax.co.uk/2014/offline-reading) [Pocket](https://getpocket.com/@ben.doherty), other people who read my stuff might do the same.\n\nI contacted Pocket support and they said that their browser extensions don't emit an event when it's clicked[^1]. I was hoping to be able to drive a Google analytics goal when that happened.\n\nPocket suggests that I look at their [Publisher stats][2], which is nice, but it's two sources of information. I wanted to be able to do it all in Google Analytics.\n\nI hacked the following together from the [MDN MutationObserver][1] docs, but it relies on Pocket not changing the classname of their UI. (And leaving it in the DOM, not as a separate overlay.) I left it [on Stack Overflow](http://stackoverflow.com/questions/37036606/can-a-save-to-pocket-be-detected) for a week to see if anyone was going to call me out for being a chump. They didn't; I guess the internet had better things to do this week.\n\n```js\ndocument.addEventListener(\"DOMContentLoaded\", function () {\n    //your other events events...\n    //ga('send', 'event', [eventCategory], [eventAction], [eventLabel], [eventValue], [fieldsObject]);\n\n    function registerPocketSave() {\n        console.log(\"looking for Pocket saves\");\n        // This is for Chrome only :(\n        var target = document.querySelector(\"body\");\n\n        // create an observer instance\n        var observer = new MutationObserver(function (mutations) {\n            mutations.forEach(function (mutation) {\n                //console.log(mutation);\n                let a = document.getElementById(\"pkt_ext_master\");\n                if (a) {\n                    console.log(a, \"found a Pocket save - firing a GA event!\");\n                    ga(\"send\", \"event\", \"Pocketed\", document.title);\n                    observer.disconnect(); //once we've saved to Pocket, stop so that we don't get ghost versions\n                }\n            });\n        });\n\n        // configuration of the observer:\n        var config = { attributes: true, childList: true, characterData: true };\n\n        // pass in the target node, as well as the observer options\n        observer.observe(target, config);\n    }\n    registerPocketSave();\n});\n```\n\nIt's actually running on this page right now, and it sort of works. It's only for Chrome. The Chrome extension mofifies the DOM and injects a popup, but the Firefox extension uses the new DOM provided by the extension's own little window.\n\nIf anyone has any suggestions then I'd love to hear about it!\n\n[1]: https://developer.mozilla.org/en/docs/Web/API/MutationObserver\n[2]: http://getpocket.com/publisher/\n\n[^1]: Actually I only asked about their Chrome extension, but I'm guessing that's common to Firefox too. Pocket support were actually really helpful about this.\n",
 "content_html": "<h1>Trying to capture a 'Save to Pocket' event</h1>\n<p>I wanted to be able to track how many people save articles to Pocket from this site. I thought that given how much <a href=\"http://notionparallax.co.uk/2014/offline-reading\">I use</a> <a href=\"https://getpocket.com/@ben.doherty\">Pocket</a>, other people who read my stuff might do the same.</p>\n\n<p>I contacted Pocket support and they said that their browser extensions don’t emit an event when it’s clicked<sup id=\"fnref:1\" role=\"doc-noteref\"><a href=\"https://notionparallax.co.uk/2016/trying-to-capture-a-save-to-pocket-event#fn:1\" class=\"footnote\" rel=\"footnote\">1</a></sup>. I was hoping to be able to drive a Google analytics goal when that happened.</p>\n\n<p>Pocket suggests that I look at their <a href=\"http://getpocket.com/publisher/\">Publisher stats</a>, which is nice, but it’s two sources of information. I wanted to be able to do it all in Google Analytics.</p>\n\n<p>I hacked the following together from the <a href=\"https://developer.mozilla.org/en/docs/Web/API/MutationObserver\">MDN MutationObserver</a> docs, but it relies on Pocket not changing the classname of their UI. (And leaving it in the DOM, not as a separate overlay.) I left it <a href=\"http://stackoverflow.com/questions/37036606/can-a-save-to-pocket-be-detected\">on Stack Overflow</a> for a week to see if anyone was going to call me out for being a chump. They didn’t; I guess the internet had better things to do this week.</p>\n\n<div class=\"language-js highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"nb\">document</span><span class=\"p\">.</span><span class=\"nx\">addEventListener</span><span class=\"p\">(</span><span class=\"dl\">\"</span><span class=\"s2\">DOMContentLoaded</span><span class=\"dl\">\"</span><span class=\"p\">,</span> <span class=\"kd\">function</span> <span class=\"p\">()</span> <span class=\"p\">{</span>\n    <span class=\"c1\">//your other events events...</span>\n    <span class=\"c1\">//ga('send', 'event', [eventCategory], [eventAction], [eventLabel], [eventValue], [fieldsObject]);</span>\n\n    <span class=\"kd\">function</span> <span class=\"nx\">registerPocketSave</span><span class=\"p\">()</span> <span class=\"p\">{</span>\n        <span class=\"nx\">console</span><span class=\"p\">.</span><span class=\"nx\">log</span><span class=\"p\">(</span><span class=\"dl\">\"</span><span class=\"s2\">looking for Pocket saves</span><span class=\"dl\">\"</span><span class=\"p\">);</span>\n        <span class=\"c1\">// This is for Chrome only :(</span>\n        <span class=\"kd\">var</span> <span class=\"nx\">target</span> <span class=\"o\">=</span> <span class=\"nb\">document</span><span class=\"p\">.</span><span class=\"nx\">querySelector</span><span class=\"p\">(</span><span class=\"dl\">\"</span><span class=\"s2\">body</span><span class=\"dl\">\"</span><span class=\"p\">);</span>\n\n        <span class=\"c1\">// create an observer instance</span>\n        <span class=\"kd\">var</span> <span class=\"nx\">observer</span> <span class=\"o\">=</span> <span class=\"k\">new</span> <span class=\"nx\">MutationObserver</span><span class=\"p\">(</span><span class=\"kd\">function</span> <span class=\"p\">(</span><span class=\"nx\">mutations</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n            <span class=\"nx\">mutations</span><span class=\"p\">.</span><span class=\"nx\">forEach</span><span class=\"p\">(</span><span class=\"kd\">function</span> <span class=\"p\">(</span><span class=\"nx\">mutation</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n                <span class=\"c1\">//console.log(mutation);</span>\n                <span class=\"kd\">let</span> <span class=\"nx\">a</span> <span class=\"o\">=</span> <span class=\"nb\">document</span><span class=\"p\">.</span><span class=\"nx\">getElementById</span><span class=\"p\">(</span><span class=\"dl\">\"</span><span class=\"s2\">pkt_ext_master</span><span class=\"dl\">\"</span><span class=\"p\">);</span>\n                <span class=\"k\">if</span> <span class=\"p\">(</span><span class=\"nx\">a</span><span class=\"p\">)</span> <span class=\"p\">{</span>\n                    <span class=\"nx\">console</span><span class=\"p\">.</span><span class=\"nx\">log</span><span class=\"p\">(</span><span class=\"nx\">a</span><span class=\"p\">,</span> <span class=\"dl\">\"</span><span class=\"s2\">found a Pocket save - firing a GA event!</span><span class=\"dl\">\"</span><span class=\"p\">);</span>\n                    <span class=\"nx\">ga</span><span class=\"p\">(</span><span class=\"dl\">\"</span><span class=\"s2\">send</span><span class=\"dl\">\"</span><span class=\"p\">,</span> <span class=\"dl\">\"</span><span class=\"s2\">event</span><span class=\"dl\">\"</span><span class=\"p\">,</span> <span class=\"dl\">\"</span><span class=\"s2\">Pocketed</span><span class=\"dl\">\"</span><span class=\"p\">,</span> <span class=\"nb\">document</span><span class=\"p\">.</span><span class=\"nx\">title</span><span class=\"p\">);</span>\n                    <span class=\"nx\">observer</span><span class=\"p\">.</span><span class=\"nx\">disconnect</span><span class=\"p\">();</span> <span class=\"c1\">//once we've saved to Pocket, stop so that we don't get ghost versions</span>\n                <span class=\"p\">}</span>\n            <span class=\"p\">});</span>\n        <span class=\"p\">});</span>\n\n        <span class=\"c1\">// configuration of the observer:</span>\n        <span class=\"kd\">var</span> <span class=\"nx\">config</span> <span class=\"o\">=</span> <span class=\"p\">{</span> <span class=\"na\">attributes</span><span class=\"p\">:</span> <span class=\"kc\">true</span><span class=\"p\">,</span> <span class=\"na\">childList</span><span class=\"p\">:</span> <span class=\"kc\">true</span><span class=\"p\">,</span> <span class=\"na\">characterData</span><span class=\"p\">:</span> <span class=\"kc\">true</span> <span class=\"p\">};</span>\n\n        <span class=\"c1\">// pass in the target node, as well as the observer options</span>\n        <span class=\"nx\">observer</span><span class=\"p\">.</span><span class=\"nx\">observe</span><span class=\"p\">(</span><span class=\"nx\">target</span><span class=\"p\">,</span> <span class=\"nx\">config</span><span class=\"p\">);</span>\n    <span class=\"p\">}</span>\n    <span class=\"nx\">registerPocketSave</span><span class=\"p\">();</span>\n<span class=\"p\">});</span>\n</code></pre></div></div>\n\n<p>It’s actually running on this page right now, and it sort of works. It’s only for Chrome. The Chrome extension mofifies the DOM and injects a popup, but the Firefox extension uses the new DOM provided by the extension’s own little window.</p>\n\n<p>If anyone has any suggestions then I’d love to hear about it!</p>\n\n<div class=\"footnotes\" role=\"doc-endnotes\">\n  <ol>\n    <li id=\"fn:1\" role=\"doc-endnote\">\n      <p>Actually I only asked about their Chrome extension, but I’m guessing that’s common to Firefox too. Pocket support were actually really helpful about this. <a href=\"https://notionparallax.co.uk/2016/trying-to-capture-a-save-to-pocket-event#fnref:1\" class=\"reversefootnote\" role=\"doc-backlink\">&#8617;</a></p>\n    </li>\n  </ol>\n</div>\n",
 "content_hash": "sha256:e183909fdae651c9db6023f1b41c418bf2b65d6eab15f221d38b502eab14e6ea",
 "media": [],
 "changelog": [
  {
   "version": 1,
   "at": "2016-05-20T17:08:27Z",
   "note": null
  }
 ],
 "transclusions": []
}