I am new to google analytics and google tag manager and would like to ask for some help with sending data to google analytics. Currently I am tracking clicks and page views, but I can not figure out how to send data from my app (for example user_id).
That’s a tough spot, and you’re running into the exact issue many of us hit when mixing a Python-first framework like Anvil with client-side JavaScript tracking. Pushing correctly to the global dataLayer from the js module can definitely be tricky to get reliable!
You’re trying to solve this on the frontend, but I’d strongly suggest looking at Server-Side GTM (sGTM) as your long-term solution. It simplifies your Anvil code immensely because you only need the main GTM container snippet, and then all your critical data pushing (like user_id) can be done via simple, reliable HTTP requests straight from your Python server code. This completely bypasses the complexities of dataLayer manipulation on the client side.
Setting up sGTM usually involves manually configuring cloud services, but there’s a new tool called the GTM Setup Assistant that can automate the entire initial infrastructure setup.
Has anyone in the Anvil community used the Setup Assistant yet to deploy their sGTM setup? It really seems like the way to avoid the whole dataLayer.push() mess, and I’m curious if it plays well with Anvil’s structure.
Hey there,
It was a long time ago, since I had this issue. I can not remember much of the details but I believe it was solved by adding a simple js function into the standard-page.html, which I called, whenever I wanted and pass some data to the GTM. Since it worked, I stopped looking around for other solutions.
function pushUserIdToDataLayer(user_id) {
window.dataLayer.push({
'userId': user_id,
'event': 'login'
});
}