- This topic has 12 replies, 3 voices, and was last updated 2 years, 7 months ago by Leo.
February 21, 2018 at 2:26 am #501772Edin
What would be the preferred way if I wanted to add tracking code (one part in the header, the other part below the body tag) to specific pages or posts only?
Like for example the FB tracking code and event tracking that I only want for a few pages of my site.
EdinGeneratePress 2.0.2February 21, 2018 at 9:02 am #502118LeoStaffCustomer Support
Something like this should help: https://docs.generatepress.com/article/using-hooks-conditional-tags/
Let me know.February 21, 2018 at 11:17 am #502251Edin
Thank you Leo.
So, if I wanted to add a tracking code to pages 55, 56, 57 in the header, I add this code to the wp_head hook:
<?php if ( is_page(55, 56, 57) ) : ?> <script>Tracking Script</script> <?php endif; ?>
I’d have to check “Execute PHP”, would that mess up code that’s already in there but is no php?
EdinFebruary 21, 2018 at 3:22 pm #502366LeoStaffCustomer SupportSeptember 17, 2018 at 8:12 am #679328Edin
I’ve had this working with one page since I started this thread.
Today, however, I wanted to add another page, but for some reason, the tracking code only fires on the first specified page number.
<?php if ( is_page(7134, 6891) ) : ?> <!-- Facebook Pixel Code --> <script> FB code </script> <noscript><img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=x" /></noscript> <!-- End Facebook Pixel Code --> <?php endif; ?>
So the FB-Pixel only fires on post 7134, NOT on post 6891. Any idea why?
EdinSeptember 17, 2018 at 9:11 am #679377DavidStaffCustomer Support
Try using the new Elements module for Hooks:
You can remove the conditional PHP now and use the Display Rules it provides.September 17, 2018 at 9:16 am #679385Edin
Can you point me to the article that describes how to switch from the “old” hooks system to the new elements module for Hooks? (I’ve seen it somewhere, but can’t find it again)September 17, 2018 at 9:24 am #679398Edin
Also, can I use both systems at the same time?September 17, 2018 at 9:31 am #679401LeoStaffCustomer Support
Should be in our documentation page:
Technically you can use both but I don’t see any advantages of doing that though.
Best to use the new one 🙂September 17, 2018 at 11:04 am #679472Edin
I started to migrate from the GP-Hooks module to the new Elements module.
Problem is, the GP-Hooks module link disappears from the dashboard as soon as “Elements” is activated. Which makes it kind of hard to copy over the code …September 17, 2018 at 11:37 am #679493LeoStaffCustomer SupportSeptember 17, 2018 at 11:42 am #679500Edin
Perfect! Thank you!September 17, 2018 at 12:19 pm #679529
- You must be logged in to reply to this topic.