Not compatible with WPML
-
Hi
When I say taht it not compatible with WPML I do not talk about string translation, thoses one works.I’m talking about the codes and snippets that are injected in the <head>
We are using WPML on seperate domains, ex:
http://www.myfrenchdomain.fr
http://www.myenglishdomain.fr
Both are the same website with WPML> Unfortunately the code snippet that is loaded is always the same, even If i enter to differents snippet in each language…
-
Hi @pako69,
Thanks for your comments.
I can confirm only the strings and text inputs are supported for WMPL, the code snippets are defined and loaded globally for each site the same.
To customise the code snippets per language site basis you can use our hooks & filters described in the “Help” section of the plugin.
An example could be (to extend scripts in 3rd party head section):add_action('moove_gdpr_third_party_header_assets','moove_gdpr_third_party_header_assets'); function moove_gdpr_third_party_header_assets( $scripts ) { if ( ICL_LANGUAGE_CODE === 'fr' ) : $scripts .= '<script>console.log("third-party-head FRENCH");</script>'; elseif ( ICL_LANGUAGE_CODE === 'en' ) : $scripts .= '<script>console.log("third-party-head ENGLISH");</script>'; endif; return $scripts; }Note: After adding the code snippets, don’t forget to save the settings in the CMS to purge the script cache. This filter might require some development input on your end, this is just an example, the ICL_LANGUAGE_CODE should be defined correctly, and the scripts added to the function.
I hope this helps.
Thank you for you help, I tried but it always return : third-party-head FRENCH
🙁Ahh, I see, this is because of script caching.
Please add the following code snippet to your functions.php to prevent the script caching:
add_action('gdpr_cookie_script_cache','gdpr_prevent_script_cache'); function gdpr_prevent_script_cache() { return array(); }I look forward to hearing from you soon
Hi, yes it works, but I’m facing another issue.
here what I test:
add_action('moove_gdpr_third_party_header_assets','moove_gdpr_third_party_header_assets'); function moove_gdpr_third_party_header_assets( $scripts ) { if ( ICL_LANGUAGE_CODE === 'fr' ) : $scripts .= '<script async src="https://www.googletagmanager.com/gtag/js?id=UA-FRENCH"></script>'; $scripts .= '<script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag("js", new Date()); gtag("config", "UA-FRENCH", { "link_attribution": true, "anonymize_ip": true }); if (window.performance) { var timeSincePageLoad = Math.round(performance.now()); gtag("event", "timing_complete", { "name": "load", "value": timeSincePageLoad, "event_category": "JS Dependencies" }); } </script>'; elseif ( ICL_LANGUAGE_CODE === 'en' ) : $scripts .= '<script async src="https://www.googletagmanager.com/gtag/js?id=UA-ENGLISH"></script>'; endif; return $scripts; }> But when I look at the console source code with the inspector, only the first line is echoing:
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-FRENCH"></script>But not the rest of the G.A. Tag
Thanks
and another issue is that if I use your snippet to load 3rd party in head section, I’m not supposed to use the textarea inputs to put some script because I will load it with your snippet BUT… your textarea do not allow to be empty…
Hi
I tried with another real website (I was trying with a stagging website).
Here is the sinppet I’m using:
add_action('gdpr_cookie_script_cache','gdpr_prevent_script_cache'); function gdpr_prevent_script_cache() { return array(); } add_action('moove_gdpr_third_party_header_assets','moove_gdpr_third_party_header_assets'); function moove_gdpr_third_party_header_assets( $scripts ) { if ( ICL_LANGUAGE_CODE === 'fr' ) : $scripts .= " <script async src='https://www.googletagmanager.com/gtag/js?id=UA-4452875-6'></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'UA-4452875-6', { 'link_attribution': true, 'anonymize_ip': true }); if (window.performance) { var timeSincePageLoad = Math.round(performance.now()); gtag('event', 'timing_complete', { 'name': 'load', 'value': timeSincePageLoad, 'event_category': 'JS Dependencies' }); } </script> "; elseif ( ICL_LANGUAGE_CODE === 'en' ) : $scripts .= " <script async src='https://www.googletagmanager.com/gtag/js?id=UA-4452875-32'></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'UUA-4452875-32', { 'link_attribution': true, 'anonymize_ip': true }); if (window.performance) { var timeSincePageLoad = Math.round(performance.now()); gtag('event', 'timing_complete', { 'name': 'load', 'value': timeSincePageLoad, 'event_category': 'JS Dependencies' }); } </script> "; endif; return $scripts; }The UA-4452875-6 is the French G.A.
The UA-4452875-32 is the English G.A.This works on the french side, but not on the english side, I get an error int he console:
Error: remote script failed https://www.googletagmanager.com/gtag/js?id=UA-4452875-6 - https://www.dimosoftware.com/wp-content/plugins/gdpr-cookie-compliance/dist/scripts/lity.js: 1 lity.js:1:2306 error https://www.dimosoftware.com/wp-content/plugins/gdpr-cookie-compliance/dist/scripts/lity.js:1 onerror https://www.dimosoftware.com/wp-content/plugins/gdpr-cookie-compliance/dist/scripts/lity.js:1 Error: remote script failed https://www.googletagmanager.com/gtag/js?id=UA-4452875-6Please help me! Thanks
Hi @pako69,
I’ve checked the code snippets and looks good. Please check my suggestions below:
1. We have a script validation in the plugin script section, so in your case you have custom codes, but the validation should pass, add the following script to the 3rd party HEAD section script (and remove everything else!): <script></script>
2. I’ve aligned the code snippet to be more comfortable for you to edit, added ob_start and ob_get_clean functions, so it seems it was a conflict with the apostrophes, now should be fine, please check the snippet below:
add_action('gdpr_cookie_script_cache','gdpr_prevent_script_cache'); function gdpr_prevent_script_cache() { return array(); } add_action('moove_gdpr_third_party_header_assets','moove_gdpr_third_party_header_assets'); function moove_gdpr_third_party_header_assets( $scripts ) { ob_start(); ?> <!-- GET THE CURRENT LANGUAGE CODE, CHECK YOUR CONSOLE! --> <script>console.log("<?php echo ICL_LANGUAGE_CODE ?>")</script> <?php if ( ICL_LANGUAGE_CODE === 'fr' ) : ?> <!-- FRENCH SCRIPTS --> <script async src='https://www.googletagmanager.com/gtag/js?id=FR_TRACKING'></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'FR_TRACKING', { 'link_attribution': true, 'anonymize_ip': true }); if (window.performance) { var timeSincePageLoad = Math.round(performance.now()); gtag('event', 'timing_complete', { 'name': 'load', 'value': timeSincePageLoad, 'event_category': 'JS Dependencies' }); } </script> <?php elseif ( ICL_LANGUAGE_CODE === 'en' ) : ?> <!-- ENGLISH SCRIPTS --> <script async src='https://www.googletagmanager.com/gtag/js?id=EN_TRACKING'></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'EN_TRACKING', { 'link_attribution': true, 'anonymize_ip': true }); if (window.performance) { var timeSincePageLoad = Math.round(performance.now()); gtag('event', 'timing_complete', { 'name': 'load', 'value': timeSincePageLoad, 'event_category': 'JS Dependencies' }); } </script> <?php endif; $scripts .= ob_get_clean(); return $scripts; }3. There is a console.log in the function to display the WPML language code for you, please check your console and align the code, if it’s displays ‘fr’ the if conditional for FR site should be “ICL_LANGUAGE_CODE === ‘fr'” etc..
I hope this helps.
and I answer you to quickly, the gdpr_prevent_script_cache() function do not changed anything, I always get ‘third-party-head FRENCH’
Stuck…
Hi
Thank you so much for your help but it do not work better > I alway get the French tracking code.You can see the live results >
https://www.dimosoftware.com/ > English
> Console echo = frhttps://www.dimosoftware.fr/ > French
> Console echo = frOr look at the <head> section too
And FYI I have this below directly in the header.php file, just to see what’s happen, and there it works:
if( ICL_LANGUAGE_CODE == 'en' ) { echo "UA-4452875-32"; } else if( ICL_LANGUAGE_CODE == 'fr' ) { echo "UA-4452875-6";}
Hi,
Sorry to hear that.. I’ve tested with WPML the code snippets and are working fine in my local machine. Probably you have a server caching and that’s why it works different on your site.
One more thing you can try, please remove the code snippets sent before from your functions.php and add the following:
add_action( 'gdpr_force_reload', '__return_true' ); add_action('wp_head', 'dimosoftware_gdpr_script_inject' ); function dimosoftware_gdpr_script_inject() { if ( function_exists( 'gdpr_cookie_is_accepted' ) && gdpr_cookie_is_accepted( 'thirdparty' ) ) : if ( defined('ICL_LANGUAGE_CODE') && ICL_LANGUAGE_CODE === 'fr' ) : ?> <!-- FRENCH SCRIPTS --> <script async src='https://www.googletagmanager.com/gtag/js?id=FR_TRACKING'></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'FR_TRACKING', { 'link_attribution': true, 'anonymize_ip': true }); if (window.performance) { var timeSincePageLoad = Math.round(performance.now()); gtag('event', 'timing_complete', { 'name': 'load', 'value': timeSincePageLoad, 'event_category': 'JS Dependencies' }); } </script> <?php elseif ( defined('ICL_LANGUAGE_CODE') && ICL_LANGUAGE_CODE === 'en' ) : ?> <!-- ENGLISH SCRIPTS --> <script async src='https://www.googletagmanager.com/gtag/js?id=EN_TRACKING'></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'EN_TRACKING', { 'link_attribution': true, 'anonymize_ip': true }); if (window.performance) { var timeSincePageLoad = Math.round(performance.now()); gtag('event', 'timing_complete', { 'name': 'load', 'value': timeSincePageLoad, 'event_category': 'JS Dependencies' }); } </script> <?php endif; endif; }This is a PHP checker, so I hope this could work!
😎
Yes it seems to work!
I re enabled my LiteSpeed cache plugin, and it still working too. I will wait to test everything to confirm you that it is finally working.
you saved my life! Thank you so much.
Perhaps it will be a grat idea to give those snippet for other peopel in your FAQ?
I go further this morning to use a more compact and simple code, that way it can be used with a lot of languages without having a long long snippet :
add_action( 'gdpr_force_reload', '__return_true' ); add_action('wp_head', 'my_gdpr_script_inject' ); function my_gdpr_script_inject() { if ( function_exists( 'gdpr_cookie_is_accepted' ) && gdpr_cookie_is_accepted( 'thirdparty' ) ) : if ( defined('ICL_LANGUAGE_CODE') && ICL_LANGUAGE_CODE === 'fr' ) : $ua_gcode = "UA-FR_TRACKING"; elseif( defined('ICL_LANGUAGE_CODE') && ICL_LANGUAGE_CODE === 'en' ) : $ua_gcode = "UA-EN_TRACKING"; endif; ?> <!-- BEGIN G.A. SCRIPTS --> <script async src='https://www.googletagmanager.com/gtag/js?id=<?php echo $ua_gcode; ?>'></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', '<?php echo $ua_gcode; ?>', { 'link_attribution': true, 'anonymize_ip': true }); if (window.performance) { var timeSincePageLoad = Math.round(performance.now()); gtag('event', 'timing_complete', { 'name': 'load', 'value': timeSincePageLoad, 'event_category': 'JS Dependencies' }); } <!-- END G.A. SCRIPTS --> <?php endif; }-
This reply was modified 7 years, 3 months ago by
pako69.
-
This reply was modified 7 years, 3 months ago by
The topic ‘Not compatible with WPML’ is closed to new replies.