Log shiny app visitors and R usage to Google Analytics
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
- If someone visits a page in your web application (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
- Do you want to know which user inputs are set in your Shiny app, you can now collect these events easily with this R package
Track usage of your scripts / package usage / functions
- Keep track on how your internal useRs are using your package (e.g. when a user loads your package or uses a specific function or webservice)
- Do you want to keep track on the status of a long-running process or keep track of an error message if something failed.
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
- Logged pageviews can be viewed in the Google Analytics > Behaviour tab or in the Real-Time part of Google Analytics
- Logged events can be viewed in the Google Analytics > Behaviour > Events tab or in the Real-Time part of Google Analytics
Enjoy!
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.