{"action":"created","issue":{"url":"https://api.github.com/repos/h5bp/html5-boilerplate/issues/1660","labels_url":"https://api.github.com/repos/h5bp/html5-boilerplate/issues/1660/labels{/name}","comments_url":"https://api.github.com/repos/h5bp/html5-boilerplate/issues/1660/comments","events_url":"https://api.github.com/repos/h5bp/html5-boilerplate/issues/1660/events","html_url":"https://github.com/h5bp/html5-boilerplate/pull/1660","id":54350206,"number":1660,"title":"Further optimizing Google Analytics snippet","user":{"login":"BLooperZ","id":10459948,"avatar_url":"https://avatars.githubusercontent.com/u/10459948?v=3","gravatar_id":"","url":"https://api.github.com/users/BLooperZ","html_url":"https://github.com/BLooperZ","followers_url":"https://api.github.com/users/BLooperZ/followers","following_url":"https://api.github.com/users/BLooperZ/following{/other_user}","gists_url":"https://api.github.com/users/BLooperZ/gists{/gist_id}","starred_url":"https://api.github.com/users/BLooperZ/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/BLooperZ/subscriptions","organizations_url":"https://api.github.com/users/BLooperZ/orgs","repos_url":"https://api.github.com/users/BLooperZ/repos","events_url":"https://api.github.com/users/BLooperZ/events{/privacy}","received_events_url":"https://api.github.com/users/BLooperZ/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/h5bp/html5-boilerplate/labels/enhancement","name":"enhancement","color":"009800"},{"url":"https://api.github.com/repos/h5bp/html5-boilerplate/labels/html","name":"html","color":"7846b9"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":24,"created_at":"2015-01-14T17:23:41Z","updated_at":"2015-04-20T17:08:03Z","closed_at":"2015-04-20T17:08:03Z","pull_request":{"url":"https://api.github.com/repos/h5bp/html5-boilerplate/pulls/1660","html_url":"https://github.com/h5bp/html5-boilerplate/pull/1660","diff_url":"https://github.com/h5bp/html5-boilerplate/pull/1660.diff","patch_url":"https://github.com/h5bp/html5-boilerplate/pull/1660.patch"},"body":"__EDIT__: it started to be a real mess in this post so i cleared it up\r\n\r\nthis is a shorter (and still safe) version of the google analytics snippet - 322 charcters against 346\r\nit does exactly the same - except adding the script after the first script in the document rather than before it. only one function changed - `insertBefore` to `appendChild` to eliminate the need for one of the parameters, all the rest is done by syntax manipulation __ONLY__\r\nthis is a bit more readable version (same code just added whitespace:\r\n```\r\n(function (b, o, i, l) {\r\n    GoogleAnalyticsObject = l;\r\n    (b[l] = b[l] || function () {\r\n        (b[l].q = b[l].q || []).push(arguments)}\r\n    ).l = +new Date;\r\n    o.getElementsByTagName(i)[0].parentNode.appendChild(o.createElement(i))\r\n    .src='//www.google-analytics.com/analytics.js'}\r\n(window, document, 'script', 'ga'));ga('create', 'UA-XXXXX-X', 'auto');ga('send', 'pageview');\r\n```\r\nlet go line by line:\r\n__new__ `(function (b, o, i, l) {` __old__ `(function(b,o,i,l,e,r)`\r\nnot much to say we need less parameters\r\n__new__ `GoogleAnalyticsObject = l;` __old__ `b.GoogleAnalyticsObject=l;`\r\nthe window object is assumed if no object specified (except for conditional statements)\r\n__new__ `(b[l] = b[l] || function () {` __old__ `b[l] || (b[l] = function () {`\r\nshorter syntax and we can use it 2 lines later\r\n__new__ `(b[l].q = b[l].q || []).push(arguments)}` __old__ `(b[l].q = b[l].q || []).push(arguments)}`\r\nno change here\r\n__new__ `).l = +new Date;` __old__ `);b[l].l = +new Date;`\r\nwe use the functio0n it self as the object instead of referencing it\r\n__new__ \r\n```\r\no.getElementsByTagName(i)[0].parentNode.appendChild(o.createElement(i))\r\n.src='//www.google-analytics.com/analytics.js'}\r\n```\r\n__old__ \r\n```\r\ne=o.createElement(i);r=o.getElementsByTagName(i)[0];\r\ne.src='//www.google-analytics.com/analytics.js';\r\nr.parentNode.insertBefore(e,r)}\r\n```\r\nby changing to appendChild we do not need reference anymore and we declare anything inside the functions - using the original value and saving the need for a parameter\r\n__new__ `(window, document, 'script', 'ga'));ga('create', 'UA-XXXXX-X', 'auto');ga('send', 'pageview');` __old__ `(window, document, 'script', 'ga'));ga('create','UA-XXXXX-X','auto');ga('send','pageview');`\r\nalso the same\r\n\r\nI've checked this script with google analytics debugger, also with multiple trackers\r\nit works (no reason why not)\r\n@mathiasbynens - feel free to update your article about it if you like\r\n"},"comment":{"url":"https://api.github.com/repos/h5bp/html5-boilerplate/issues/comments/94511094","html_url":"https://github.com/h5bp/html5-boilerplate/pull/1660#issuecomment-94511094","issue_url":"https://api.github.com/repos/h5bp/html5-boilerplate/issues/1660","id":94511094,"user":{"login":"alrra","id":1223565,"avatar_url":"https://avatars.githubusercontent.com/u/1223565?v=3","gravatar_id":"","url":"https://api.github.com/users/alrra","html_url":"https://github.com/alrra","followers_url":"https://api.github.com/users/alrra/followers","following_url":"https://api.github.com/users/alrra/following{/other_user}","gists_url":"https://api.github.com/users/alrra/gists{/gist_id}","starred_url":"https://api.github.com/users/alrra/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alrra/subscriptions","organizations_url":"https://api.github.com/users/alrra/orgs","repos_url":"https://api.github.com/users/alrra/repos","events_url":"https://api.github.com/users/alrra/events{/privacy}","received_events_url":"https://api.github.com/users/alrra/received_events","type":"User","site_admin":false},"created_at":"2015-04-20T17:08:03Z","updated_at":"2015-04-20T17:08:03Z","body":"Thanks @mathiasbynens! :purple_heart:\r\n\r\n--\r\n\r\nClosing in favor of #1696. "}}