Site logo

Archived topic

Tracking Google Ads Events on a Category and Thank You Page

42 replies · Started by Georgi on December 1, 2022

Viewing posts 1–15 of 43

Hello,

Finally bought the lifetime subscription. Yay!

I'm running a Google Ads for only 1 category of my website and instead of cluttering the site the normal way by installing Google Tag Manager to make Google Ads tracking code fire only on that category and thank you page, I was wondering if this is possible to achieve with GeneratePres hooks?

Hi there,

yes, you can use a GP Hook Element to hook in your codes.
Simply set the display rules for the specific pages, posts or archives that you want the code to fire.

So Tracking codes are generally added in one of 3 places, and those and their equivalent hooks are:

1. In the <head> of the site - use: wp_head
2. After the opening body tag - use: wp_body_open
3. Before the closing body tag - use: wp_footer

For example GTM generally has 2 scripts that use head and body. See here for more info:

https://docs.generatepress.com/article/implement-google-tag-manager/

I see.

But if I want to fire tags only on certain categories, can I implement the GTM to be displayed only on these certain categories? Or I need to set its code to be displayed on the entire site ?

Those hooks are present on every page.
So you need to set the Elements Display Rules for just the places you want.

eg. Posts in a specific category you would set: Post Category -> Category Term

Then that hook will only load on posts of that term.

Great, so I will implement GTM only on certain categories, not on the whole site, so it doesn't slow the site speed too much.

Also, I was wondering - do the Hooks by themselves increase requests on page? Or they have no influence, only the code they execute has influence?

Nope, Hooks are simply "placeholders", they do nothing apart from inserting the code what you add inside one.

Thank you, David, you're clarifying a lot of things for me!

One more thing - can I put the preconnect attribute together with the code for Analytics in the same hook?

<link href=’https://www.google-analytics.com’ rel=’preconnect’ crossorigin>

Before the code or after the code for analytics I can put it, do you know?

For the analytics script that goes in the wp_head hook then Yes you can include it inside the same hook. And it should come before the analytics script.

Okay.

I now noticed that while I can specify in a Hook for the Google Tag Manager scripts to run on a particular category, there's no option to make them work also on the thank you page.

Do you know how can they be executed particularly on the thank you page, not the entire site?

What is the thank you page ? Is it coming from Woocommerce or another plugin ?

It's from WooCommerce.

Ok, so there isn't a 'real page' for that, its a URL endpoint.
It will need some PHP to add the element to that.

1. Edit your current Hook, and get its ID from the browser URL.
Keep the current Display Rules for the other places you need it on.

2. Now add this PHP Snippet to your site:


add_filter( 'generate_element_display', function( $display, $element_id ) {
    if ( 100 === $element_id && is_wc_endpoint_url('order-received') ) {
        $display = true;
    }

    return $display;
}, 10, 2 );

Where it is 100 change this to the Elements ID.

Thank you a lot, David!
I haven't tried this yet but I don't think there will be a problem.
I will mark the thread as resolved.

This archived topic is closed to new replies.