<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/log-shiny-app-visitors-and-r-usage-to-google-analytics/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.bnosac.be/index.php/blog/73-log-shiny-app-visitors-r-events-and-r-usage-to-google-analytics"> bnosac :: open analytical helpers</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>If you work on applications for clients or have open sourced some shiny apps, a question that arises is <strong>how is your application being used</strong>. What you can do in order to find out how your hard work is being consumed is putting your code in logs and then viewing the logs.</p>
<p>An easier way however to track usage of your application is just sending page views or application events to Google Analytics. That’s exactly what the GAlogger R package (<a rel="nofollow" href="https://github.com/bnosac/GAlogger" target="_blank">https://github.com/bnosac/GAlogger</a>) is doing. It allows to <strong>log R events and R usage to Google Analytics</strong> and was created with the following use cases in mind:</p>
<h3>Track usage of your application</h3>
<ul>
<li>If someone <strong>visits a page in your web application</strong> (e.g. Shiny) or web service (e.g. RApache, Plumber), use the GAlogger R package to send the page and title of the page which is visited so that you can easily see how visitors are using your application</li>
<li>Do you want to know which <strong>user inputs</strong> are set in your Shiny app, you can now collect these events easily with this R package</li>
</ul>
<h3>Track usage of your scripts / package usage / functions</h3>
<ul>
<li>Keep track on <strong>how your internal useRs are using your package</strong> (e.g. when a user loads your package or uses a specific function or webservice)</li>
<li>Do you want to <strong>keep track </strong>on the<strong> status </strong>of a long-running<strong> process </strong>or<strong> keep track of an error </strong>message if something failed.</li>
</ul>
<p><a rel="nofollow" href="https://github.com/bnosac/GAlogger" target="_blank"><img alt="GALogger screenshot 1" src="https://i2.wp.com/www.bnosac.be/images/bnosac/blog/GAlogger-screenshot-1.png?w=456" data-recalc-dims="1"></a></p>
<h3>How</h3>
<p>First of all, get the R package from <a rel="nofollow" href="https://github.com/bnosac/GAlogger" target="_blank">https://github.com/bnosac/GAlogger</a></p>
<p>Get your own free tracking ID from <a rel="nofollow" href="https://www.google.com/analytics" target="_blank">Google Analytics</a> (it looks like UA-XXXXX-Y), set it as shown below and indicate that you approve that data will be send to Google Analytics. Put that code in your shiny app or R script.</p>
<pre><code>library(GAlogger)
ga_set_tracking_id(&quot;UA-25938715-4&quot;)
ga_set_approval(consent = TRUE)</code></pre>
<p>Next start sending data to Google Analytics. You can either send <strong>page visits</strong> or <strong>events</strong>.</p>
<h3>Page visits</h3>
<blockquote>
<p>Someone is visiting your web service or shiny web application, great, log it is follows.</p>
</blockquote>
<pre><code>ga_collect_pageview(page = &quot;/home&quot;)
ga_collect_pageview(page = &quot;/simulation&quot;, title = &quot;Mixture process&quot;)
ga_collect_pageview(page = &quot;/simulation/bayesian&quot;)
ga_collect_pageview(page = &quot;/textmining-exploratory&quot;)
ga_collect_pageview(page = &quot;/my/killer/app&quot;)
ga_collect_pageview(page = &quot;/home&quot;, title = &quot;Homepage&quot;, hostname = &quot;www.xyz.com&quot;)</code></pre>
<h3>Events</h3>
<blockquote>
<p>An event is happening in your app or R code, great, log it is follows.</p>
</blockquote>
<pre><code>ga_collect_event(event_category = &quot;Start&quot;, event_action = &quot;shiny app launched&quot;)
ga_collect_event(event_category = &quot;Error&quot;, event_label = &quot;convergence failed&quot;, event_action = &quot;Oh no&quot;)
ga_collect_event(event_category = &quot;Error&quot;, event_label = &quot;Bad input&quot;, 
                 event_action = &quot;send the firesquad&quot;, event_value = 911)
ga_collect_event(event_category = &quot;Simulation&quot;, event_label = &quot;Launching Bayesian multi-level model&quot;,
                 event_action = &quot;How many simulations&quot;, event_value = 10)</code></pre>
<h3>Visit Google Analytics to see who visited you or what happened in your script</h3>
<ul>
<li>Logged pageviews can be viewed in the Google Analytics > Behaviour tab or in the Real-Time part of Google Analytics</li>
<li>Logged events can be viewed in the Google Analytics > Behaviour > Events tab or in the Real-Time part of Google Analytics</li>
</ul>
<p>Enjoy!</p>
<p><a rel="nofollow" href="https://github.com/bnosac/GAlogger" target="_blank"><img alt="GALogger screenshot 2" src="https://i0.wp.com/www.bnosac.be/images/bnosac/blog/GAlogger-screenshot-2.png?w=456" data-recalc-dims="1"></a></p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.bnosac.be/index.php/blog/73-log-shiny-app-visitors-r-events-and-r-usage-to-google-analytics"> bnosac :: open analytical helpers</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/1YqnAqovd_Y" height="1"></a></div>