I have the following html code to embed the Cloudflare Insights javascript into my website.
<script defer src='https://static.cloudflareinsights.com/beacon.min.js' data-cf-beacon='{"token": "001e2db377654356d03acf35f1e5a"}'></script>
Because of the cookie law in the EU, I only want to load the script when someone accepts cookies. Therefore I’ve a javascript function where I load those third-party scripts only when someone accepts cookies.
How can I embed the Cloudflare Insights script including sending the data-cf-beacon data with my javascript function? My function now loads the cloudflare script, but doesn’t send the data-cf-beacon.
function BlockCookie() {
var imported = document.createElement('script');
imported.src = 'https://static.cloudflareinsights.com/beacon.min.js';
document.head.appendChild(imported);
} ```