Reactivity Problem: Google Analytcs event tracking clashes with Previous and Next buttons


#1

Hi guys!

I have an issue with my shiny dashboard app.

So this is the app mudrost.shinyapps.io/BuyOrRent/ where I have two actionButton buttons (you can see them at the top middle of the app) with id`s "Previous" and "Next".

I use these buttons to navigate the sidebarMenu which has id = "tabs."

The code below is included in the server function in the app.

--- The back and next buttons ---

tab_id <- c("welcome", "the_decision", "age", "partner_status", "live_together_new_place", 
    "live_together_old_place", "rent_apt_price", "living_situation_4", "property_location", 
    "mortgage_details", "future_situation", "outcome", "simulation_1", "simulation_2",
    "simulation_3", "simulation_4", "simulation_5", "simulation_6", "simulation_7",
    "simulation_8")

observe({
    lapply(c("Next", "Previous"),
    toggle,
    condition = input[["tabs"]] != "Home")
})

Current <- reactiveValues(
    Tab = "Home"
)

observeEvent(
    input[["tabs"]],
    {
        Current$Tab <- input[["tabs"]]
    }
)

observeEvent(
    input[["Previous"]],
    {
        tab_id_position <- match(Current$Tab, tab_id) - 1
        if (tab_id_position == 0) tab_id_position = 1 # <- length(tab_id)
        Current$Tab <- tab_id[tab_id_position]
        updateTabItems(session, "tabs", tab_id[tab_id_position]) 
    }
)

observeEvent(
    input[["Next"]],
    {
        tab_id_position <- match(Current$Tab, tab_id) + 1
        # The if statement is like this to prevent going fromt last to 1st page
        if (tab_id_position > length(tab_id)) tab_id_position <- tab_id_position - 1
        Current$Tab <- tab_id[tab_id_position]
        updateTabItems(session, "tabs", tab_id[tab_id_position]) 
    }
)

The above works fine. However, I tried adding google analytics tracking. When I add the following event tracking code in dashboardBody, then the Previous and Next buttons stop working, i.e. I can click them, but they don`t lead you to previous/next page.

    tags$script(HTML(
        "$(document).on('shiny:inputchanged', function(event) {
        trackingFunction('trackEvent',
        'level1', 'level2', 'IsSingle', 'YES');
        });"
    )),

I suspect there is a fundamental problem with how I use reactivity for the Previous and Next buttons. Can anybody help? Thanks!!!


#2

Hi --

I'm not sure if you've seen this article/approach -- https://shiny.rstudio.com/articles/google-analytics.html -- but I found it extremely helpful in getting my Shiny GA event tracking to work. I then used the "googleAuthR" and "googleAnalyticsR" packages to make sense of the tracking data.

Best!


#3

Thanks, that approach seems to work!!!


#4

If your question's been answered, would you mind choosing a solution? (see FAQ below for how) It makes it a bit easier to visually navigate the site and see which questions still need help.

Thanks