Site icon R-bloggers

Log shiny app visitors and R usage to Google Analytics

[This article was first published on bnosac :: open analytical helpers, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

If you work on applications for clients or have open sourced some shiny apps, a question that arises is how is your application being used. 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.

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 (https://github.com/bnosac/GAlogger) is doing. It allows to log R events and R usage to Google Analytics and was created with the following use cases in mind:

Track usage of your application

Track usage of your scripts / package usage / functions

How

First of all, get the R package from https://github.com/bnosac/GAlogger

Get your own free tracking ID from Google Analytics (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.

library(GAlogger)
ga_set_tracking_id("UA-25938715-4")
ga_set_approval(consent = TRUE)

Next start sending data to Google Analytics. You can either send page visits or events.

Page visits

Someone is visiting your web service or shiny web application, great, log it is follows.

ga_collect_pageview(page = "/home")
ga_collect_pageview(page = "/simulation", title = "Mixture process")
ga_collect_pageview(page = "/simulation/bayesian")
ga_collect_pageview(page = "/textmining-exploratory")
ga_collect_pageview(page = "/my/killer/app")
ga_collect_pageview(page = "/home", title = "Homepage", hostname = "www.xyz.com")

Events

An event is happening in your app or R code, great, log it is follows.

ga_collect_event(event_category = "Start", event_action = "shiny app launched")
ga_collect_event(event_category = "Error", event_label = "convergence failed", event_action = "Oh no")
ga_collect_event(event_category = "Error", event_label = "Bad input", 
                 event_action = "send the firesquad", event_value = 911)
ga_collect_event(event_category = "Simulation", event_label = "Launching Bayesian multi-level model",
                 event_action = "How many simulations", event_value = 10)

Visit Google Analytics to see who visited you or what happened in your script

Enjoy!

To leave a comment for the author, please follow the link and comment on their blog: bnosac :: open analytical helpers.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.