meom / meom-3rd-party-scripts Goto Github PK
View Code? Open in Web Editor NEWWordPress plugin for creating settings for 3rd party script like Google Tag Manager and Cookiebot.
WordPress plugin for creating settings for 3rd party script like Google Tag Manager and Cookiebot.
There is interesting approach to delay 3. party JS.
I'm not suggesting to delay our JS by default but document how it can be done.
src
attributes to delay
attributes.We already have filters for this:
/**
* Delay JS by changing `src=` to `delay=` in GTM and Cookiebot.
*
* @link https://blog.speedvitals.com/delay-javascript/
*
* @return void
*/
function delay_js( $code, $id ) {
return str_replace( 'src=', 'delay=', $code );
}
add_filter( 'mtps_gtm_head_code', 'delay_js', 10, 2 );
add_filter( 'mtps_cookiebot_code', 'delay_js', 10, 2 );
I'm testing a little bit modified version of the JS:
/**
* Change `delay` attributes back to `src` attributes.
*
* @return void
*/
function print_delay_js() {
?>
<script>
var delayLoaded = false;
var autoLoadDuration = 5;
var eventList = ["keydown", "mousemove", "wheel", "touchmove", "touchstart", "touchend"];
var autoLoadTimeout = setTimeout(runScripts, autoLoadDuration * 1000);
eventList.forEach(function(event) {
window.addEventListener(event, triggerScripts, { passive: true })
});
function triggerScripts() {
runScripts();
clearTimeout(autoLoadTimeout);
eventList.forEach(function(event) {
window.removeEventListener(event, triggerScripts, { passive: true });
});
}
function runScripts() {
if (!delayLoaded) {
document.querySelectorAll("script[data-cookieconsent]").forEach(function(scriptTag) {
<?php // Create new script tag and replace "delay=" with "src=". ?>
var newNode = document.createElement('script');
newNode.setAttribute("data-cookieconsent", "ignore");
var newContent = scriptTag.textContent.replace("delay=", "src=");
newNode.textContent = newContent;
scriptTag.parentNode.replaceChild(newNode, scriptTag);
});
document.querySelectorAll("script[delay]").forEach(function(scriptTag) {
<?php // Replace "delay=" with "src=" attribute. ?>
scriptTag.setAttribute("src", scriptTag.getAttribute("delay"));
});
}
delayLoaded = true;
}
</script>
<?php
}
add_action( 'wp_head', 'print_delay_js' );
In the above code I'm using document.querySelectorAll("script[data-cookieconsent]")
to detect GTM code. It doesn't feel like bullet proof because we might not even have Cookiebot on the page.
We should have our own data attribute, for example data-meom-delay
and hook into that:
document.querySelectorAll("script[data-meom-delay]")
.
@giraffewoods Should we add short info how to install via composer?
The CookieDeclaration script looks like this:
<script id="CookieDeclaration" src="https://consent.cookiebot.com/xxxx/cd.js" type="text/javascript" async></script>
It would be nice to have a shortcode or something so we don't have to add the script to the editor.
@SinttiSalminen you had some insight how to add the language version to the script?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.