{"id":651,"date":"2016-10-30T10:27:21","date_gmt":"2016-10-30T09:27:21","guid":{"rendered":"http:\/\/www.voldrich.net\/?p=651"},"modified":"2020-03-27T17:00:21","modified_gmt":"2020-03-27T15:00:21","slug":"google-analytics-report-parsing-in-java-8","status":"publish","type":"post","link":"https:\/\/www.voldrich.net\/index.php\/2016\/10\/30\/google-analytics-report-parsing-in-java-8\/","title":{"rendered":"Google analytics report parsing in Java 8"},"content":{"rendered":"<p>Google has deprecated Google Analytics API v3 in favor of its new reporting API (V4). New API is quite universal and easy to use and its java library gets the job done well with one exception: mapping result to Java types. It returns everything in strings and you have to parse the Reports yourself and map the values to your object. I recently had to load quite a lot of daily statistics from google analytics so I wrote a simple class which does this dirty work for me.<\/p>\n<p><!--more--><\/p>\n<p>For a class with fields annotated with metric annotation. <a href=\"https:\/\/developers.google.com\/analytics\/devguides\/reporting\/core\/dimsmets\" target=\"_blank\" rel=\"noopener noreferrer\">You can find all allowed metric values here<\/a>.\u00a0Note that it always uses ga:date as dimension.<\/p>\n<p>[sourcecode language=&#8221;java&#8221;]<br \/>\npublic class SessionStats extends BaseDailyStats {<br \/>\n@GoogleAnalyticsMetric(&quot;ga:sessions&quot;)<br \/>\nprivate Long sessions = 0L;<\/p>\n<p>@GoogleAnalyticsMetric(&quot;ga:bounces&quot;)<br \/>\nprivate Long bounces = 0L;<\/p>\n<p>@GoogleAnalyticsMetric(&quot;ga:bounceRate&quot;)<br \/>\nprivate Double bounceRatePercent = 0d;<\/p>\n<p>@GoogleAnalyticsMetric(&quot;ga:avgSessionDuration&quot;)<br \/>\nprivate Double avgSessionDuration = 0d;<\/p>\n<p>@GoogleAnalyticsMetric(&quot;ga:hits&quot;)<br \/>\nprivate Long hits = 0L;<br \/>\n}<br \/>\n[\/sourcecode]<\/p>\n<p>The code is invoked by calling:<\/p>\n<p>[sourcecode language=&#8221;java&#8221;]<br \/>\nClient client = new Client(analyticsReporting, VIEW_ID, MAX_NUMBER_OF_DAYS);<br \/>\nList&lt;SessionStats&gt; dailyStats = client.getDailyStats(SessionStats.class);<br \/>\ndailyStats.forEach(System.out::println);<br \/>\n[\/sourcecode]<\/p>\n<p>It requests statistics for number of days until present day and automatically binds them to the Stats class.<\/p>\n<p>Easy and quick. You can find the source code on github.<br \/>\n<a href=\"https:\/\/github.com\/MavoCz\/googleAnalyticsEasyReport\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/github.com\/MavoCz\/googleAnalyticsEasyReport<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Google has deprecated Google Analytics API v3 in favor of its new reporting API (V4). New API is quite universal and easy to use and its java library gets the job done well with one exception: mapping result to Java types. It returns everything in strings and you have to parse the Reports yourself and [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"templates\/template-full-width.php","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-651","post","type-post","status-publish","format-standard","hentry","category-development"],"_links":{"self":[{"href":"https:\/\/www.voldrich.net\/index.php\/wp-json\/wp\/v2\/posts\/651","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.voldrich.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.voldrich.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.voldrich.net\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.voldrich.net\/index.php\/wp-json\/wp\/v2\/comments?post=651"}],"version-history":[{"count":1,"href":"https:\/\/www.voldrich.net\/index.php\/wp-json\/wp\/v2\/posts\/651\/revisions"}],"predecessor-version":[{"id":701,"href":"https:\/\/www.voldrich.net\/index.php\/wp-json\/wp\/v2\/posts\/651\/revisions\/701"}],"wp:attachment":[{"href":"https:\/\/www.voldrich.net\/index.php\/wp-json\/wp\/v2\/media?parent=651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.voldrich.net\/index.php\/wp-json\/wp\/v2\/categories?post=651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.voldrich.net\/index.php\/wp-json\/wp\/v2\/tags?post=651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}