• Members 24 posts
    Sept. 21, 2023, 8:34 a.m.

    I aimed to integrate Google Analytics 4 into my WordPress website without resorting to any unnecessary plugins. Therefore, I followed this guide: [link to the guide].

    The guide is straightforward to follow, but the final step is quite perplexing. It instructs, "Paste your Google tag immediately after the <head> on each page of your website."

    This directive left me wondering about its practical implementation. If you're using a file manager (in my case, One.com), it's unclear where exactly the Google tag should be inserted.

    I attempted to add it to the "header.php" file within the active theme I'm using, but unfortunately, it didn't yield the desired results.

    Could you offer any advice or suggestions to help with this issue?

  • Mod
    Sept. 21, 2023, 8:46 a.m.

    I understand not using a plugin. However, utilizing a plugin like Site Kit is essentially equivalent to manually inserting the code into your site's header via your hosting file manager. The advantage of using Site Kit is that it enables you to access Analytics data for your site directly from your WordPress admin panel, eliminating the need to switch between platforms.

    If you do prefer the manual placement of code snippets, the header of your site is typically defined within your theme files. In most cases, you'll locate the header in the "header.php" file, although it's advisable to confirm this with your theme provider.

    One important consideration when manually adding your snippet to your theme files is that unless you are using a child theme, any updates to your theme may overwrite your changes, necessitating the reinsertion of the snippet. Therefore, I recommend checking your theme settings; there might be an option to safely input your Analytics snippet.

  • Members 24 posts
    Sept. 21, 2023, 8:48 a.m.

    Thankyou very much for your response. I indeed attempted to insert the code into the "header.php" file, but unfortunately, it didn't establish a connection with GA4 as expected. I'm uncertain about the root cause of this issue, so I've reached out to my hosting provider, One.com, for assistance. I will keep you updated if they provide any valuable insights.

    It's possible that I may eventually resort to using a plugin after all. I had previously utilized MonsterInsights, but I uninstalled it due to an excessive amount of ads and concerns about its impact on site performance.

    Is it conceivable that the complexity of the instruction, "Paste your Google tag immediately after the <head> on each page of your website," leads most individuals to eventually opt for a plugin to avoid the challenges associated with manual implementation?

  • Members 24 posts
    Sept. 21, 2023, 8:49 a.m.

    I've received a response from One.com support, and unfortunately, they couldn't provide a standard solution for syncing GA4 with a WordPress.org site. Consequently, I decided to opt for a plugin and chose Site Kit, as it was recommended earlier.

    However, for reasons I can't quite pinpoint, Site Kit failed to establish a connection with my site for both GA4 and Google Search Console. I used the same Google account that I registered these two services with.

    The error message I encountered stated "invalid client_id" and "invalid client_secret" (you can refer to the screenshot below). When I clicked on "Remake configuration" (the green button), a completely black page appeared with the following text in the upper left corner: "invalid parameter: 'client_id'."

    This experience doesn't align with the "installation instructions" provided for setting up Site Kit with GA4. Instead, it immediately displays error messages after I activate the plugin and sign in with my Google ID.

    Do you have any insights into what might be causing this issue? Thanks!

  • Mod
    Sept. 21, 2023, 8:51 a.m.
  • Members 24 posts
    Sept. 21, 2023, 8:52 a.m.

    I opted to experiment with another plugin that appeared to be more lightweight and had favorable reviews. This plugin is named "GA Google Analytics" (you can find it at this link: wordpress.org/plugins/ga-google-analytics/). After installing it and adding my tracking code, it functioned smoothly without any complications.

    Hence, I've decided to continue using this plugin, unless there are significant advantages to using Site Kit that I might be missing out on.

  • Members 37 posts
    Sept. 21, 2023, 8:54 a.m.

    I make use of the WordPress plugin GTM4WP, which allows you to input your GTM container ID without the need for manual code installation. Simply access the WordPress plugin menu, search for GTM4WP, and you're all set. Additionally, this plugin includes some handy data layer options, particularly beneficial if you're not accustomed to configuring them for GA4 events and other functions. You can find the plugin here: wordpress.org/plugins/duracelltomi-google-tag-manager/

  • Mod
    Sept. 21, 2023, 8:55 a.m.

    @mmadhu @pgpt

    Check if this works -

    function gtm_tag_head_setting() {
    echo "<!-- Google Tag Manager -->
    <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
    new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
    j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
    'www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
    })(window,document,'script','dataLayer','GTM-...(your tag id)...');</script>
    <!-- End Google Tag Manager -->";
    }
    add_action( 'wp_head', 'gtm_tag_head_setting', 2 );

    Place this code in your functions.php