Who Touched My Shiny-app?

[This article was first published on Category: R | Huidong Tian's Blog, 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.

When we created a Shiny-app, deployed it on a server and open it to public, we must have interesting of who visited our app, and if possible, where they from and what they have done. To achieve this, we need a user behavior tracking feature if we are not using the commercial version: Shiny-server pro. I wrote a JavaScript code to conduct such operation, and would like to share with you.

The idea of the JavaScript code is:

  1. Trigger action when user change inputs.
  2. Catch the changed object, get its value and label, and put it in a Shiny input.
  3. Export the value stored in above input to local file.

So, we need some JavaScript to catch user’s behavior and store the value and label to the new created Shiny input.

“` ruby // Who touched my Shiny-app? $(document).on(“change”, “.shiny-bound-input:not([id=’tracking’])”, function(evt) { var el = $(evt.target); if (el.prop(“tagName”).toLowerCase() === “select”) { value = $(“option:selected”, el).map(function(){ return this.text }).get().join(“, “); label = el.prev(“label”).text(); } else if (el.attr(“type”) === “checkbox”) { value = el.attr(“checked”); label = el.parent().text(); } else if (el.attr(“type”) === “radio”) { value = el.next().text(); label = $(“.control-label”, el.parent().parent()).text(); } else if (el.attr(“type”) === “text”) { value = el.val(); console.log(el.prev(“label”)) label = el.prev(“label”).text(); if (el.attr(“class”) === “input-small”) { label = label + el.index() } } else if (el.attr(“type”) === “number”) { value = el.val(); label = el.prev(“label”).text(); }; label = label.replace(/\n|\r/gm,””); label = label.replace(/^ *| *$/gm,””); var target = $(“#tracking”); target.val(label + “|” + value); target.trigger(“change”); });

“`

And, we need users’ location and geographic coordinates. we can get them from many IP query websites, like ipinfo.io.

ruby // IP info; $(document).ready(function(){ var target = $("#tracking"); $.get("http://ipinfo.io", function(response) { target.val("ipInfo|" + response.ip + "," + response.city + "," + response.loc); target.trigger("change"); }, "jsonp"); });

A demo can be found here, and its source codes are stored here.

To leave a comment for the author, please follow the link and comment on their blog: Category: R | Huidong Tian's Blog.

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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)