{"id":5155,"date":"2016-10-19T01:33:40","date_gmt":"2016-10-19T01:33:40","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=5155"},"modified":"2016-10-19T01:33:40","modified_gmt":"2016-10-19T01:33:40","slug":"recording-site-behavior-google-analytics","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/recording-site-behavior-google-analytics\/","title":{"rendered":"Recording on-site behavior in Google Analytics"},"content":{"rendered":"<p>Google Analytics has a ton of built-in features<sup><a href=\"#footnote_0_5155\" id=\"identifier_0_5155\" class=\"footnote-link footnote-identifier-link\" title=\"https:\/\/developers.google.com\/analytics\/devguides\/collection\/analyticsjs\/field-reference#content\">1<\/a><\/sup> to help you learn how people are using your applications. To take full advantage of this, I found it necessary to follow a guide<sup><a href=\"#footnote_1_5155\" id=\"identifier_1_5155\" class=\"footnote-link footnote-identifier-link\" title=\"https:\/\/www.optimizesmart.com\/complete-guide-to-dimensions-and-metrics-in-google-analytics\/\">2<\/a><\/sup>.<\/p>\n<p>For my research project (a <a href=\"https:\/\/www.findlectures.com\">lecture search engine<\/a>), I&#8217;d like to see a report of queries people run, and how good the results are (# of results, # that can be played inline, # where authors have written books). Like so:<br \/>\n<img alt='' class='alignnone size-full wp-image-5166' src='http:\/\/172.104.26.128\/wp-content\/uploads\/2016\/10\/img_5806c8be4bd57.png' \/><\/p>\n<p>To set this up, you&#8217;ll want to first create named custom metrics and dimensions you that want inside Google Analytics. I don&#8217;t think this is strictly necessary, but it makes the analytics UI show your names. &#8220;Metrics&#8221; are numeric measures you want to track (e.g. number of results), and dimensions are attributes of an event or page view (query, time of day, etc).<br \/>\n<img alt='' class='alignnone size-full wp-image-5165' src='http:\/\/172.104.26.128\/wp-content\/uploads\/2016\/10\/img_5806c72f94b55.png' \/><\/p>\n<p>Each dimension \/ metric is numbered (dimension1, dimension2, &#8230;) so you&#8217;ll want to keep careful track of these, and not re-purpose them in your site. Not that these values are scoped to &#8220;hit&#8221;, &#8220;session&#8221;, &#8220;product&#8221;, or &#8220;user&#8221;, the difference between these should be relatively apparent.<\/p>\n<p>To record these values, you need to add some Javascript to your pages. I recommend surrounding this in a try\/catch, so that you don&#8217;t throw errors, to respect your users with aggressive ad blockers. I would also blank out the values you don&#8217;t use, because they will otherwise be sent on subsequent events and cause confusion.<\/p>\n<pre lang=\"javascript\">\ntry {\n  ga('set', 'dimension1', selections.query);\n  ga('set', 'dimension2', selections.page);\n  ga('set', 'dimension3', null);\n  ga('set', 'dimension4', null);\n  ga('set', 'dimension5', null);\n            \n  ga('set', 'metric1', (data.response || {}).numFound);\n  ga('set', 'metric2', numWithPlayer);\n  ga('set', 'metric3', numWithAmazon);\n\n  ga('send', 'pageview');\n} catch (e) {\n}\n<\/pre>\n<p>To see this working, you can look at the network traffic in your browser. You will see attributes named &#8220;cd&#8221; and &#8220;cm&#8221; (custom dimension and custom metric). The original Google Analytics didn&#8217;t have real time metrics, and some portions still take a while to show up &#8211; this is one of them. While some people report that custom values show up within 1-2 hours, it took about 12 for me.  If they don&#8217;t show up at all, it&#8217;s possible your code is not calling their API in the right order<sup><a href=\"#footnote_2_5155\" id=\"identifier_2_5155\" class=\"footnote-link footnote-identifier-link\" title=\"http:\/\/stackoverflow.com\/questions\/23528368\/google-analytics-custom-dimension-not-being-set\">3<\/a><\/sup><\/p>\n<pre>\ncd1:test\ncd2:1\ncm1:13\ncm2:13\ncm3:0\n<\/pre>\n<p>You can also record event data, which is pretty slick, e.g.:<br \/>\n<img alt='' class='alignnone size-full wp-image-5167' src='http:\/\/172.104.26.128\/wp-content\/uploads\/2016\/10\/img_5806cb087f3da.png' \/><\/p>\n<p>When you drill into the secondary dimensions, you get a nice view. The challenge here is planning out what dimensions go in each slot (you don&#8217;t want the first index to be sometimes a URL and sometimes a query, if possible, to avoid reports that mix these). I also found that if you have two values, like the name of a checked field and it&#8217;s value, that there doesn&#8217;t seem to be a way to report on both at once. The solution to this may be to add a third value that is both, so that you get the choice later.<br \/>\n<img alt='' class='alignnone size-full wp-image-5168' src='http:\/\/172.104.26.128\/wp-content\/uploads\/2016\/10\/img_5806cc2727631.png' \/><\/p>\n<p>This requires more Javascript, similar to the previous example:<\/p>\n<pre lang=\"javascript\">\ntry {\n  ga('set', 'dimension1', null);\n  ga('set', 'dimension2', null);\n  ga('set', 'dimension3', null);\n  ga('set', 'dimension3', incomingFacetConfig.field);\n  ga('set', 'dimension4', incomingFacetValue);\n\n  ga('set', 'metric1', null);\n  ga('set', 'metric2', null);\n  ga('set', 'metric3', null);\n            \n  ga('send', 'event', 'facet');\n} catch (e) {\n}\n<\/pre>\n<p>Finally, if you just want to track queries, Google Analytics has a nice built-in feature for that, but you have to enable it. The setting is in the administration panels for your site &#8211; enable &#8220;Site Search Tracking&#8221; and set the query parameter, and in a few hours, you&#8217;ll start to get data.<\/p>\n<p><img alt='' class='alignnone size-full wp-image-5171' src='http:\/\/172.104.26.128\/wp-content\/uploads\/2016\/10\/img_5806ccad184da.png' \/><\/p>\n<ol class=\"footnotes\"><li id=\"footnote_0_5155\" class=\"footnote\">https:\/\/developers.google.com\/analytics\/devguides\/collection\/analyticsjs\/field-reference#content<span class=\"footnote-back-link-wrapper\"> [<a href=\"#identifier_0_5155\" class=\"footnote-link footnote-back-link\">&#8617;<\/a>]<\/span><\/li><li id=\"footnote_1_5155\" class=\"footnote\">https:\/\/www.optimizesmart.com\/complete-guide-to-dimensions-and-metrics-in-google-analytics\/<span class=\"footnote-back-link-wrapper\"> [<a href=\"#identifier_1_5155\" class=\"footnote-link footnote-back-link\">&#8617;<\/a>]<\/span><\/li><li id=\"footnote_2_5155\" class=\"footnote\">http:\/\/stackoverflow.com\/questions\/23528368\/google-analytics-custom-dimension-not-being-set<span class=\"footnote-back-link-wrapper\"> [<a href=\"#identifier_2_5155\" class=\"footnote-link footnote-back-link\">&#8617;<\/a>]<\/span><\/li><\/ol>","protected":false},"excerpt":{"rendered":"<p>Customizing Google Analytics to track behavior on a search engine<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[12],"tags":[47],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/5155"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=5155"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/5155\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=5155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=5155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=5155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}