Skip Navigation

This thread is resolved. Here is a description of the problem and solution.

Problem:
The client has a tracking form that works on the English version of their website but does not track when the Spanish language is selected. The tracking code in the head tag disappears on the translated Spanish version of the site.

Solution:
If you're experiencing a similar issue with tracking code not functioning correctly in all languages on your website, we recommend setting up the tracking code individually for each language. You can use the "wpml_current_language" hook provided by WPML to customize code for different languages. Here's a step-by-step guide:

1. Refer to our documentation on how to use the "wpml_current_language" hook: https://wpml.org/wpml-hook/wpml_current_language/

2. Add the following PHP code snippet to the functions.php file of your theme:

$my_current_lang = apply_filters( 'wpml_current_language', NULL );<br /><br />if($my_current_lang=='es-ES'){<br />//Here goes the tracking code.<br />}

3. Ensure that you use "echo" to output the JavaScript code within the PHP "if" statement, like this:

$my_current_lang = apply_filters( 'wpml_current_language', NULL );<br /><br />if ($my_current_lang == 'es-ES') {<br />    echo '<script>"use strict";var simplyconvert_hash = "5bde78b7cdb60170c883efdd3ee8cde9";<br />    !function() {var e = 3e5, n = Math.ceil(new Date() / e) * e, o = document.createElement("script");<br />    o.id = "_scloader", o.type = "text/javascript", o.async = !0, o.src = "//simplyconvert.com/app/embed/"+n+"/load.js";<br />    var i = document.getElementsByTagName("script")[0];i.parentNode.insertBefore(o, i);<br />    }();</script>';<br />}

By following these steps, the tracking code should work for the Spanish version of your site as well.

If this solution doesn't look relevant to your issue, please feel free to open a new support ticket with us: Contact WPML Support

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by dagobertoR 1 year, 6 months ago.

Assisted by: Prosenjit Barman.

Author Posts
December 12, 2023 at 9:06 pm #15049835

dagobertoR

I am trying to: We have a tracking form that tracks any form on our website. However, we've discovered it will not track the form when the Spanish language is selected.

We have this tracking code in the head tag, which works for the main English site. But this code disappears for the translated Spanish version of the site.

Here's the code that is in on the head tag in English:

<head> tag of the spanish pages:
<script>"use strict";var simplyconvert_hash = '5bde78b7cdb60170c883efdd3ee8cde9';!function() {var e = 3e5, n = Math.ceil(new Date() / e) * e, o = document.createElement("script");o.id = "_scloader", o.type = "text/javascript", o.async = !0, o.src = "//simplyconvert.com/app/embed/"+n+"/load.js";var i = document.getElementsByTagName("script")[0];i.parentNode.insertBefore(o, i);}();</script>

Link to a page where the issue can be seen: hidden link

I expected to see:

Instead, I got:

December 13, 2023 at 11:42 am #15054101

Prosenjit Barman
WPML Supporter since 03/2023

Languages: English (English )

Timezone: Asia/Dhaka (GMT+06:00)

Hello There,
Thanks for contacting WPML Support.

I understand the issue you're having and will try my best to assist you in this matter.

For the tracking code to function correctly in each language on your website, it's necessary to set up the tracking code individually for every language. This approach ensures accurate and language-specific tracking.

WPML provides a useful hook named "wpml_current_language" that allows you to customize code for different languages. To understand how to effectively use this hook, you can refer to the following documentation: https://wpml.org/wpml-hook/wpml_current_language/

With this hook, you can specifically set up the tracking code for the Spanish version of your site. Here’s a sample code snippet:

 $my_current_lang = apply_filters( 'wpml_current_language', NULL );
  
if($my_current_lang=='es-ES'){
//Here goes the tracking code.
}

This code should be added to the functions.php file of your theme.

By implementing this, you should be able to make the tracking code work appropriately for the Spanish language as well. Feel free to let me know if you need further assistance in this matter. I will be happy to assist.

Best regards,
Prosenjit

December 16, 2023 at 7:47 pm #15077887

dagobertoR

Thank you for the reply. I entered the following code into my Functions.php file, but I code the error:
"Your PHP code changes were not applied due to an error on line 57 of file wp-content/themes/Avada/functions.php. Please fix and try saving again.
syntax error, unexpected token "if" "

Line 57 refers to this line:

if($my_current_lang=='es-ES')

Here's the entire code I entered into my Functions.php file:

$my_current_lang = apply_filters( 'wpml_current_language', NULL );
   
if($my_current_lang=='es-ES'){
//
<script>"use strict";var simplyconvert_hash = '5bde78b7cdb60170c883efdd3ee8cde9';
!function() {var e = 3e5, n = Math.ceil(new Date() / e) * e, o = document.createElement("script");
o.id = "_scloader", o.type = "text/javascript", o.async = !0, o.src = "//simplyconvert.com/app/embed/"+n+"/load.js";
var i = document.getElementsByTagName("script")[0];i.parentNode.insertBefore(o, i);
}();</script>
}
December 18, 2023 at 4:04 am #15080263

Prosenjit Barman
WPML Supporter since 03/2023

Languages: English (English )

Timezone: Asia/Dhaka (GMT+06:00)

Hi There,

The issue with your PHP code seems to be the placement of JavaScript within the PHP "if" statement. In PHP files, you need to use "echo" to output the JavaScript code. Here's how you can modify your code in the functions.php file to fix the syntax error:

$my_current_lang = apply_filters( 'wpml_current_language', NULL );

if ($my_current_lang == 'es-ES') {
    echo '<script>"use strict";var simplyconvert_hash = "5bde78b7cdb60170c883efdd3ee8cde9";
    !function() {var e = 3e5, n = Math.ceil(new Date() / e) * e, o = document.createElement("script");
    o.id = "_scloader", o.type = "text/javascript", o.async = !0, o.src = "//simplyconvert.com/app/embed/"+n+"/load.js";
    var i = document.getElementsByTagName("script")[0];i.parentNode.insertBefore(o, i);
    }();</script>';
}

Please try and let me know if you need further assistance in this matter. I will be happy to help.

Best regards,
Prosenjit

December 18, 2023 at 5:19 pm #15086511

dagobertoR

I placed the code you sent above, and while I get no errors, it doesn't work. Here's an image that might help.

The image titled 'Free case evaluation' shows the form on the bottom right hand side of the screen.

The image titled 'Spanish' is our Spanish version of the site, and you'll see the 'Free case evaluation' form is not being shown.

The code for this form must be in the <head> tag. And it is located there for the English page. However, when you click on Spanish, the code is no longer in the head tag area.

Does that shed any more insight into this issue? thank you again... we are really lost with this issue.

Spanish page.png
Free case evaluation.png
December 18, 2023 at 7:27 pm #15087269

dagobertoR

I figured it out! I went to the backend of the translated Spanish page and inserted the code there.