Get help from the marimo community

Updated 7 months ago

Google Tag Manager on Marimo

At a glance

The community member is asking how to cleanly install Google Tag Manager (GTM) on a webpage using Marimo. A community member suggests that the only way to execute custom scripts in Marimo is through the mo.ui.anywidget function, but they are not 100% sure if this would work for installing GTM. The community member encourages the original poster to try this approach and let them know how it goes.

Useful resources
To install GTM on a webpage, the following code needs to be injected just under <head>

<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-***');</script>
<!-- End Google Tag Manager -->


Is there a clean way to do this in Marimo?
A
2 comments
The only place we allow execution of custom scripts is through anywidget. You could maybe achieve this through mo.ui.anywidget (its render function is called when the widget is rendered), but I'm not 100% sure.

https://docs.marimo.io/guides/integrating_with_marimo/custom_ui_plugins.html#marimo.ui.anywidget
If you try it, let me know how it goes?
Add a reply
Sign up and join the conversation on Discord
Is there a clean way to do this in Marimo?","url":"https://community.marimo.io//google-tag-manager-on-marimo-dLw4CZy7GVTq","identifier":"dLw4CZy7GVTq","publisher":{"@type":"Organization","name":"marimo","logo":{"@type":"ImageObject","url":"https://assets.usehall.com/org_01JDJKYVWQSJ3YGE2WXS6C4F17/6e34d90a-5df6-480a-833e-17f056b6e796.png"}},"comment":[{"@type":"Comment","text":"The only place we allow execution of custom scripts is through anywidget. You could maybe achieve this through mo.ui.anywidget (its render function is called when the widget is rendered), but I'm not 100% sure.https://docs.marimo.io/guides/integrating_with_marimo/custom_ui_plugins.html#marimo.ui.anywidget","dateCreated":"2024-07-30T19:40:35.976Z","dateModified":"2024-07-30T19:40:35.976Z","author":{"@type":"Person","url":"https://community.marimo.io//members/238b9dd8-a8dd-44f6-9ec2-3360acdeda94","name":"Akshay","identifier":"238b9dd8-a8dd-44f6-9ec2-3360acdeda94","image":"https://cdn.discordapp.com/avatars/754176921591349320/b88f4bf9a0961c33d1ed5dc51754e9f3.webp?size=256"},"commentCount":0,"comment":[],"position":1,"upvoteCount":0},{"@type":"Comment","text":"If you try it, let me know how it goes?","dateCreated":"2024-07-30T20:46:02.430Z","dateModified":"2024-07-30T20:46:02.430Z","author":{"@type":"Person","url":"https://community.marimo.io//members/238b9dd8-a8dd-44f6-9ec2-3360acdeda94","name":"Akshay","identifier":"238b9dd8-a8dd-44f6-9ec2-3360acdeda94","image":"https://cdn.discordapp.com/avatars/754176921591349320/b88f4bf9a0961c33d1ed5dc51754e9f3.webp?size=256"},"commentCount":0,"comment":[],"position":2,"upvoteCount":0}],"author":{"@type":"Person","url":"https://community.marimo.io//members/2ea52cea-019c-4f56-b7ba-c13073d97824","name":"KunalM","identifier":"2ea52cea-019c-4f56-b7ba-c13073d97824","image":"https://cdn.discordapp.com/avatars/695116621437730867/514da05403e8b60a514e5d6346672b15.webp?size=256"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0}}]