Sauter la navigation

[Résolu] Error When Adding Google Analytics Code Using Code Snippet

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem: I am trying to add some Google Analytics code in a Toolset Custom Code snippet but it's not working as expected.

Solution: Toolset's custom code snippets feature is for adding PHP code, not JavaScript / HTML. You can follow Astra's guide for adding JavaScript at the link below.

Relevant Documentation:
https://wpastra.com/docs/add-custom-javascript-code-astra/

This support ticket is created Il y a 5 années et 1 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Ce sujet contient 2 réponses, a 2 voix.

Dernière mise à jour par barryG-4 Il y a 5 années et 1 mois.

Assisté par: Christian Cox.

Auteur
Publications
#1388925

I am trying to: add google analytics code using Toolset - Settings - Custom Code but when I save and test I get the following error:

Snippet "google-analytics" has been updated.
There was error when trying to re-run the snippet:syntax error, unexpected '<', expecting end of file in /home/kingslover/public_html/wp-content/toolset-customizations/google-analytics.php on line 9 A problem occurred when executing snippet "google-analytics". The result of include_once is: ""

The code I am adding is:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="lien caché"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'UA-5054472-130');
</script>

I tried removing what I thought might be unnecessary white spaces but that didn't change anything.

I'd be grateful if anyone can tell me what might be wrong with the code. The site is a parent theme so I cannot add it to functions.php like I would normally do, as it would get wiped out by any updates.

I cloned this site using Duplicator Pro from my test domain at whurld.com. The Toolset plugins say they are registered but they are not showing in the site list inside my Toolset admin page so is it not working because of something to do with needing the Toolset plugins to be properly associated with this domain (kingslover.com).

Many thanks,
Barry.

#1388927

Hi, the snippet you're trying to add is HTML and JavaScript code, which is executed in the browser. Toolset's custom code snippet feature is for PHP code, which is executed on the server, so HTML and JavaScript aren't supported directly like this. It looks like you're using the Astra theme with Astra Pro, so you should be able to follow their recommendations here for adding a custom JavaScript snippet: https://wpastra.com/docs/add-custom-javascript-code-astra/

I would give that a shot first. Otherwise, you'll need to enqueue it in a child theme, or find another 3rd-party solution for enqueueing custom HTML and JavaScript.

#1388929

Hi Christian,

Thank you for a super quick response. That was almost instant!!!

Many thanks,
Barry.