【问题标题】:Shiny tabPanel and Google Analytics闪亮的 tabPanel 和 Google Analytics
【发布时间】:2021-01-15 09:38:53
【问题描述】:

我已经阅读了很棒的教程here。但是,我对 jQuery 的了解为零。我在 ShinyApp 中使用了几个 tabPanel 来显示我的数据。本教程解释了如何跟踪链接点击事件(效果很好,我包含了一个 .js,如教程中所述)。有没有办法跟踪用户是否点击了特定的 tabPanel(例如 Panel1Panel2)?我尝试对引用外部资源的链接做同样的事情,但这不起作用。

tabsetPanel(
tabPanel("Panel1", showOutput("PlotPanel1", 'dimple')),
tabPanel("Panel2", showOutput("PlotPanel2", 'dimple')))

编辑:

我想我必须在我的 analytics.js 文件中包含一些代码。因此我尝试了几件事,但坦率地说,在没有 jQuery 知识的情况下,这是错误的。有人可以帮忙吗?

$( ".selector" ).tabs({
  on('option', 'click', function(l) {
  ga('send', 'event', 'tabPanel', 'tabPanel', $(l.currentTarget).val());
  }
});

谢谢。

【问题讨论】:

    标签: javascript jquery r google-analytics shiny


    【解决方案1】:

    如果我正确地得到了你需要的输出,你可以做这样的事情(我不使用 javascript):

    ui <- fluidPage(
    
      #give an id to your tab in order to monitor it in the server
      tabsetPanel(id = 'tab123',
        tabPanel("Panel1", textOutput("PlotPanel1")),
        tabPanel("Panel2", textOutput("PlotPanel2"))
      )
    
    )
    
    server <- function(input, output) {
    
      #now server can monitor the tabPanel through the id.
      #make the observer do whatever you want, e.g. print to a file
      observeEvent(input$tab123, {
        print(input$tab123)
    
        if (input$tab123 == 'Panel1') {
          sink(file = 'c:/Users/TB/Documents/panel1.txt', append = TRUE)
          cat(1)
          sink()
        } else {
          sink(file = 'c:/Users/TB/Documents/panel2.txt', append = TRUE)
          cat(1)
          sink()
        }
    
      })
    
    }
    
    shinyApp(ui, server) 
    

    首先,你给你的tabsetPanel一个id。现在服务器可以访问选项卡数据,您可以使用observeEvent 创建一个事件来观察。每次用户单击每个选项卡时,print 都会在控制台上打印选项卡名称(这是为了让您查看变量 input$tab123 包含的内容)。然后你可以用这些信息做任何你想做的事情。潜在地,将其存储在带有时间戳的数据库中。在上面的示例中,它在我的文档中创建了两个文件,每次有人单击选项卡时写入值 1。然后你只需读入文件并将它们相加。

    【讨论】:

      【解决方案2】:

      在尝试了很多不同的方法后,这个对我有用:

      $(document).on('click', 'a', function(e) {
          ga('send', 'event', 'TabsetPanel', 'Tab Viewed', $(this).attr('data-value'));
        });
      

      虽然它会收集所有与a相关的事件,但我猜只有标签有数据值,其余的会显示为未设置。

      【讨论】:

        猜你喜欢
        • 2017-05-15
        • 2018-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-01
        • 1970-01-01
        • 2016-05-03
        • 2016-07-24
        相关资源
        最近更新 更多