Populating the dataLayer using the onClick method of actionButton


#1

I have a dashboard that needs Google Tag Manager functionality. The site is here: https://demography.dola.colorado.gov/community-profiles/

The user has to select a data level, a specific location, and select a set of topic areas, then they click on a "view Profile" button to generate the selected profile. .

When the user clicks on the "View Profile" button I want to write the values of the selected data level, location and the selected topics to the dataLayer.

What is the recommended way to do this?
Can I write a JS function that is triggered by the onClick method of the actionButton command?
Should I use an onclick() function in the server portion of the Shiny code?

I'd appreciate any suggestions.
TIA
AB


#2

I'm not exactly sure what you mean by dataLayer, but I'm thinking you may not need any javascript. The question to as is "is all the data I need available in Shiny?" You should know when the button is clicked and the values of the user selections without needing any javascript. You could just create an eventReactive that updates when the user clicks the button and contains all the selection info. Then display that info in the UI or do whatever you need to with it.

If you do find you need to send data from JS to Shiny or from Shiny to JS check out these two webpages.

https://shiny.rstudio.com/articles/js-send-message.html

Cool app by the way!


#3

Thanks ablack3,
What is the dataLayer?
It's a JS array that Google Tag Manager uses to communicate tag information to its interface. https://developers.google.com/tag-manager/devguide
I was able to create a function that writes a script chunk that populates the dataLayer. The Tag Manager code then sends this to the Tag Manager application. I was wondering if there was another way to do this, but I guess the brute force method works..

Thanks for the kudos on the app.
Cheers--
AB