Google Analytics and header.php

I contacted you about putting code into site for google analytics. You gave me directions to: “You can create file _header.php in wp-content\themes\x-child\framework\legacy\cranium\headers\views\global and add you code in there.” I did that but the file _header.php blew up the site. If I rename it ti header.php without the underscore it recognizes it as far as not bringing site down, and I thought it was working. Google analytics was giving me access and showing reports but never shows more than one user and not all the time, even when I know someone was on site. I have to believe it is not working. I need assistance in getting this to work correctly. I have no idea what is not working correctly. Is there a troubleshooting procedure we can go through to figure this out? Google analytics support is completely worthless.
Thanks.
LCW

Hi @johnhansen033, why are you trying to include your Google analytics code in your header php? Why not just add the JS script to your JS script in the theme options area, or install the Google Analytics extension that comes with the X/Pro theme?

Hi @werner7 and @johnhansen033,

Thanks for writing in.

Yes, the easiest way to use the GA extension however there is an issue regarding meta details in PRO as of now. The issue is already submitted and now being processed for fixing.

For the meantime, you could follow the link below.

Hope it helps.

Let us know how it goes.

Thanks.

This is a perfect example of the frustration that I experience every single time I get involved with support. Please don’t take it the wrong way @werner7 but I am new to this and trying to learn something as I go through my issues. I have a philosophical issue with plugins in general and use them only under extreme circumstances.They are often the reason for problems and you do not learn by using them. It seems to me it is the lazy thing to do.But support has been woefully inadequate each time I require assistance. You always assume that I know much more than I do. And I will never get to learn it if it is not explained, or unfortunately, after hours of painstaking work to solve it myself when it would have taken only minutes with a good explanation from support, clear and concise, and IN_DEPTH. And visuals never hurt.
Why am I trying to put this in the header? BECAUSE support instructed me to do so in a previous request. And now we are talking about functions.php. What happened to the header.php? Hours of my time wasted. I have no idea why it didn’t work and no idea even if it could be fixed. Different support tech, different direction. @werner7 asks why I did not JUST add the JS script to the JS script in the themes area? Pretty simple. I did not know you could, I have not worked with JS and know little or nothing about it to even attempt it. So now there are three options. I just want the analytics installed and working so I can get back to content. I need someone to work with me until it is working properly and I can go on to something else. It does not seem to be an unreasonable request.
And as for your link to kinsta, excuse my ignorance once again, but what do I replace ‘your_function_name’ with? Actually the instructions are not bad, and with visuals, and yet that is not explained.
Any help would be greatly appreciated. I really do respect every minute that you put into helping. But this is extremely frustrating to me, and has been from the first dyt I purchased X. I like the software. I like my site. I absolutely dread having to contact support.
Thanks.

Hi There @johnhansen033

I certainly understand your frustration here. With regard to our Google Analytics extension, it is not compatible with Google Tag Manager code which comes with Google Analytics code. So we do not recommend to use it for now.

With regard to Analytics Code placement, you can either integrate into your header or footer area. It’s totally depending on your site specific details such as, what are the other priority scripts that you want to load on your header and whether you want to increase site loading speeds etc. To get an idea, you read more information here.

With regard to above provided link (Kinsta Reference), you can see there are two methods that you can use to integrate Google Analytics. The first method is using the plugin called Header and Footer. If you want to implement the manual method, you need to utilize the function which you can add into your child theme’s functions.php file.

Basically you can name your_function_name as any name that you would like, however it should be a unique name. So for example

add_action('wp_head', 'my_custom_google_analytics_code');
function my_custom_google_analytics_code(){
?>

If you have further confusions, you can see an example from here.

Hope that helps.

As I have already stated I am not interested in an extension or plugin. I appreciate your links but they are still incomplete. I am not sure that I understand the relationships with the function. In any case I am trying to put together a functions.php with the information to make this work. I know the site is looking at the file because it brings the site down when I make the attempt with the new file. I have included the functions.php I am working on. When I attempt to access site, I am receiving a syntax error on line 43. There is nothing even on that line so I again assume that something is missing. I hope you can see my error. Thanks

1 <?php
2
3 //

====
4 // FUNCTIONS.PHP
5 // -----------------------------------------------------------------------------
6 // Overwrite or add your own custom functions to X in this file.
7 //

====
8
9 //

====
10 // TABLE OF CONTENTS
11 // -----------------------------------------------------------------------------
12 // 01. Enqueue Parent Stylesheet
13 // 02. Additional Functions
14 //

====
15
16 // Enqueue Parent Stylesheet
17 //

====
18
19 add_filter( ‘x_enqueue_parent_stylesheet’, ‘__return_true’ );
20
21 function lcw_google_analytics () { ?>
22
23
29
40
41 add_action ( ‘wp=head’, ‘lcw_google_analytics’ );
42
43
44

Hi There,

Sorry to hear that you’re having further difficulties. Here’s a full example of functions.php file below.

<?php

// =============================================================================
// FUNCTIONS.PHP
// -----------------------------------------------------------------------------
// Overwrite or add your own custom functions to X in this file.
// =============================================================================

// =============================================================================
// TABLE OF CONTENTS
// -----------------------------------------------------------------------------
//   01. Enqueue Parent Stylesheet
//   02. Additional Functions
// =============================================================================

// Enqueue Parent Stylesheet
// =============================================================================

add_filter( 'x_enqueue_parent_stylesheet', '__return_true' );



// Additional Functions
// =============================================================================


function my_custom_google_analytics_code() { ?>

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxxxx-1"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-xxxxxxxxx-1');
</script>


<?php }

add_action( 'wp_head', 'my_custom_google_analytics_code' );

Make sure to replace UA-xxxxxxxxx-1 with your Google Analytics ID and it should function properly on your site.

Thanks!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.