Hi everyone,
I’m facing an issue in a Wappler project where I need to insert a Google Tag (gtag.js) script and pass a dynamic value, specifically the conversion_id, which is fetched directly from the database via Server Connect.
The goal is that, when the page loads, the Google Tag script should be dynamically populated with the conversion_id, which is returned from my Server Connect.
What I’ve Tried So Far:
- JavaScript function to retrieve the data:
• I attempted to create a function to fetch the data using dmx.app.data.sc_cart.data.query_gads.conversion_id, but I keep getting errors that sc_cart is not defined or that the data is not loaded.
• I also tried using the onsuccess attribute in the Server Connect to trigger the function, but the dynamic data wasn’t available when the function was called.
- Watcher to check when the data is loaded:
• I tried an approach using dmx.app.data.sc_cart.load().then() to ensure the data was available before injecting the script, but I still couldn’t get it to work.
- Different methods of structuring the script:
• I experimented with several ways to inject the conversion_id directly into the script via JavaScript, but either the script wouldn’t load or the data wasn’t being populated.
What I Need:
I need a reliable and efficient way to insert the Google Tag script with the dynamic conversion_id as soon as it is loaded by Server Connect. The value of conversion_id comes from sc_cart.data.query_gads.conversion_id.
The expected code should look something like this:
<script async src="https://www.googletagmanager.com/gtag/js?id={{ sc_cart.data.query_gads.conversion_id }}"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){ dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', '{{ sc_cart.data.query_gads.conversion_id }}');
</script>
However, I’m struggling to ensure that the dynamic value is correctly loaded.
Has anyone faced this issue or can offer any suggestions on how to resolve this integration problem with Wappler?
I’ve already tried everything mentioned in this post, but I haven’t had any success:
Google Global Site Tag (gtag.js) tracking code insertion issue](Google Global Site Tag (gtag.js) tracking code insertion issue
I’d appreciate any help!
Note: My page is a content page, so the script will be placed in the body and not in the head. My project is Node.js