Ecommerce Shopify WordPress Discussion

Print the value fetched from javascript script in php

I use following way to fetch the value of gtag if it already exists in the code: <script> const regex = /googletagmanager\.com\/gtag\/js\?id=([^\s'"]+)/gi; const scriptTags = document.querySelectorAll('script'); let trackingID = ''; scriptTags.forEach(script => { const src = script.getAttribute('src'); if (src && src.includes('googletagmanager.com/gtag/js')) { const match = regex.exec(src); if (match && match.length > 1) { trackingID = match[1]; } } }); </script> Now I want the trackingID to be printed in the following script if it exists else print the code value that exists <script async src="<?php echo ( $local_analytics_file ?? 'https://www.googletagmanager.com/gtag/js?id=' ) . '?' . esc_html( $UA_CODE ); ?>"></script> Need something like <script async src="<?php echo ( $local_analytics_file ?? 'https://www.googletagmanager.com/gtag/js?id=+if(trackingID) trackingID' ) . '?' . esc_html( $UA_CODE ); ?>"></script> And similarly in following <script> console.log('Google Analytics Tracking ID:', trackingID); window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); const configuration = JSON.parse( '<?php echo $configuration; ?>' ); const gaID = '<?php echo esc_html( $UA_CODE ); ?>'; // I want trackingID to be printed if available else print the $UA_CODE </script>
TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.