javascript - 带有 R Shiny Inputs 和 Google Analytics 的事件跟踪器
问题描述
有人知道用 Google Analytics 和 R Shiny 跟踪事件的语法吗?
我想跟踪用户在与我的应用交互时选择的输入。所以在这个例子中,我想知道用户何时使用并更改了“PointUseInput”复选框输入。
我尝试按照此处的建议进行操作,但我对 JavaScript 不是很熟悉,所以我不确定如何构造 ga 函数。
# ################################################################################################
# ################################################################################################
# # Sec 1a. Needed Libaries & Input Files
library(shiny)
library(shinydashboard)
library(leaflet)
library(dplyr)
##The Data
Map_DF <- data.frame("Point_ID" = c("A1", "B1", "C3"),
"Latitude" = c(38.05, 39.08, 40.05),
"Longitude" = c(-107.00, -107.05, -108.00),
"PointUse" = c("farm", "house", "well"))
################################################################################################
################################################################################################
#UI
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
### tags$head(includeHTML(("google-analytics.html"))), #Google Analytics html tag here
checkboxGroupInput(inputId = "PointUseInput", label = "Select Point Use", choices = Map_DF$PointUse, selected = Map_DF$PointUse)
),
dashboardBody(
fluidRow(leafletOutput(outputId = 'mapA'))
)
)
################################################################################################
################################################################################################
server <- function(input, output, session) {
## The Filter
filterdf <- reactive({
Map_DF %>%
filter(PointUse %in% input$PointUseInput)
})
## Base Map Creation
output$mapA <- renderLeaflet({
leaflet() %>%
addProviderTiles(
providers$Esri.DeLorme,
options = providerTileOptions(
updateWhenZooming = FALSE,
updateWhenIdle = TRUE)
) %>%
setView(lng = -107.50, lat = 39.00, zoom = 7)
})
## Update Map with Filter Selection
observe({
leafletProxy("mapA", session) %>%
clearMarkers() %>%
addCircleMarkers(
data = filterdf(),
radius = 10,
color = "red",
lat = ~Latitude,
lng = ~Longitude,
popupOptions(autoPan = FALSE),
popup = ~paste("PointUse: ", filterdf()$PointUse))
})
}
################################################################################################
################################################################################################
shinyApp(ui = ui, server = server)
使用随附的 google-analytics JavaScript 代码...
$(document).on('change', 'select', function(e) {
ga('send', 'event', 'category', 'action', 'label', value);
});
解决方案
在google-analytics JavaScript 代码中设置以下语法对我有用:
gtag('event', <action>, {
'event_category': <category>,
'event_label': <label>,
'value': <value>
});
所以,而不是:
$(document).on('change', 'select', function(e) {
ga('send', 'event', 'category', 'action', 'label', value);
});
尝试:
$(document).on('change', 'select', function(e) {
gtag('event', 'action', {
'event_category': 'category',
'event_label': 'label',
'value': <value>
});
});
你也可以试试:
$('#selectInputId').on('change', function(e) {
gtag('event', 'action', {
'event_category': 'category',
'event_label': 'label',
'value': <value>
});
});
这是我在关注您链接到的同一篇文章时所做的唯一更改。
推荐阅读
- r - 通过在 R 中交替其值来对向量进行排序
- c - 当我设置 ls = temp->next 时,c 中的段错误,ls、temp 都是指针等于同一个链接头指针
- basic-authentication - 在 Vapor 4 中注册新用户时如何保护我的路线?
- javascript - 按对象类别查找多个数组的深度嵌套数组数据的总和
- elasticsearch - 在 Windows 上本地凝视 kibana v 7.12.0 时出错
- python - 如何尽可能高效地在 Python 中向我的 pandas Dataframe 添加条件的、基于列表的列?
- animation - JavaFx 中的按钮动画
- automation - 如何在下面的代码中为 selenium 编写循环条件
- python - 当带有参数的发布请求起作用时,无法使用 urllib 从网站下载 csv 文件
- python - 如何检查 Python HTTP 请求中存在哪些 JSON 数据?