1

How can I disable a code from GTM on homepage? I want that code only on product pages.

Thank you

7

Gael's answer is probably the best, but i just wanted to let you know that there's also a possibility of having the GTM container code, but not actually tracking the home page. In that case, you just need to create a trigger, that would look like in the image attached.no home page trigger in GTM

  • 1
    Thanx @Mnea but not sure my answer is the best. Your solution offers the same capabilities with the advantage not to have to edit the theme's files. – gael Jul 12 at 8:39
  • It actually depends, perhaps OP wanted to speed up the load time on home page for some reason, and by excluding the container this would probably do it. I just had to mention this solution, so that everything is covered. – Mnea Jul 12 at 15:46
3

The easyest way not to display Google Tag Manager on Homepage is to check with php.

You can insert GTM in the header.php file of your child-theme like this:

<?php 
 if (!is_home() && !is_front_page()) {
   echo 'your GTM code here';
 } 
?>

Will display GTM everywhere but on home (blog posts homepage) nor on front page (the static page you may have defined as the home of your wordpress).

Then if you only want it on product pages there are a bunch of possibilities since there are a lot of product pages types (category page, single product page...).

But if you want it to display only on a few product pages type you could choose to display it only IF it's the good page type.

<?php 
 if (is_product()) {
   echo 'your GTM code here';
 } 
?>

You will find woocommerce conditional tags here : https://docs.woocommerce.com/document/conditional-tags/ where you will be able to choose on which condition you want your GTM to display or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.