GA 4 Script

Google Analytics or GA4 Instructions

“Copy and paste it in the code of every page of your website, immediately after the element. Don’t add more than one Google tag to each page”

Apologies if this has been addressed previously, Will adding this script to the head element of the main.ejs page suffice for the content.ejs pages?

Thank’s in advance!

Yes, the GA 4 code will track all the content pages linked to the main.ejs (layout page).

You can check that in the Google Analytics portal as well after adding the GA 4 in the main.ejs.

Thank You, Thank You!

You are welcome! I would also recommend setting up server side rendering of meta tags for SEO and tracking the pages with their own titles.

@guptast – I appreciate that recommendation, and was able to become comfortable with server side rendering, meta tags :wink: a while back; it took a moment!

Again, thanks – this isn’t the first time you have you provided guidance, and that is not lost on me.

1 Like

I’m happy to help! We all get better when we share our knowledge :slight_smile:

To add to @guptast’s answer, I tend to paste the code at the bottom of the page so that it does not block the rendering of the page.

The only drawback is that when a page is closed before fully rendered, it will not register in analytics. This is a matter of choice.

Thanks, Ben; I will certainly remember that. And while I am at it, your videos have been an invaluable resource, huge fan; thanks again!

1 Like

I’m circling back to this as my previous attempts were unsuccessful, and AWSTATS had satisfied most requirements. Anywho, I have a few sites that want G4 Analytics, and I am stumped. I appreciate guptast and ben’s advice, and I think guptast was trying to steer me in the right direction, alluding to the server-side rendering of meta tags; that process seemed straightforward and was accomplished a while back.

From what I’ve gathered, adding the G4 script is to be done through the DOM panel on the main.ejs page, which I’ve done, and G4 doesn’t recognize the property. I’ve tried adding the script as “source” and through the “text” field. It’s clear that the above is wrong, and I would appreciate any guidance. Adding through the "source’ or the text field produces the following:

Many thanks!

You can add (paste) any script directly in code view.

Thank you, I appreciate it! :+1:

As @Teodor has explained, you need to copy and paste similar to the following code in all of the pages that you want to track. If you are using Node, then only the layout page needs to have the code installed.

Google suggests putting this in the <header>, while I have suggested to place it at the bottom of the page to speed up the rendering.

For more info, these videos may help:
google ga4 tutorial - YouTube

Ben, thank you for your reply; you are the best. I have one down and two to go! For whatever reason, I convinced myself I had to insert the script in either the source or text field, which wasn’t working! I’m on the right path now; many thanks.

2 Likes