admin管理员组文章数量:1125040
I use following way to fetch the value of gtag if it already exists in the code:
<script>
const regex = /googletagmanager\\/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/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 ?? '=' ) . '?' . esc_html( $UA_CODE ); ?>"></script>
Need something like
<script async src="<?php echo ( $local_analytics_file ?? '=+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>
本文标签: how to assign javascript variable value to php variable in wordpress
版权声明:本文标题:how to assign javascript variable value to php variable in wordpress 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736653040a1946187.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论