问题描述
有人会知道使用Google Analytics(分析)和R Shiny跟踪事件的语法吗?
我想跟踪用户在与我的应用进行交互时正在选择的输入。因此,在此示例中,我想知道用户何时使用“ PointUseInput”复选框输入并对其进行更改。
我尝试遵循建议here,但是我对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',function(e) {
gtag('event',{
'event_category': 'category','event_label': 'label','value': <value>
});
});
您也可以尝试:
$('#selectInputId').on('change','value': <value>
});
});
这是我在跟踪链接到the same article时所做的唯一更改。