Where to Paste Google Analytics tracking code? Themeco Analytics plugin not working

Hi,

I have installed the Themeco Analytics plugin, and added the account ID, but its only showing organic visits - not paid and we have a full campaign running. So, I need to know where, and in which theme file I can paste the new Google analytics tracking code, instead of using the plugin?

I already tried pasting into the header.php, but that simply adds the code above the tags. Please advise.

Thanks!

Hello @YearRound,

Thanks for asking. :slight_smile:

You can add following code in child theme function.php file:

function third_party_tracking_code() { ?>

  <script>
   
  // Google Analytics.
  </script>

<?php }

add_action( 'wp_head', 'third_party_tracking_code' );  

Replace // Google Analytics. with the tracking code.

Thanks.

1 Like

Hi there. When I do this, it gives me this error:

Your PHP code changes were rolled back due to an error on line 42 of file wp-content/themes/x-child/functions.php. Please fix and try saving again.

syntax error, unexpected ā€˜}’, expecting end of file

Hello Andy,

Could you please post the entire contents of your child theme’s functions.php file? You might have inserted an incorrect or has missing character like quotes or semi-colons.

By the way, as an alternative, if do not want to add any custom code in the child theme’s functions.php file and would like to easily manage any script you might be adding in the header or footer, please install ā€œInsert Headers and Footersā€ plugin (https://wordpress.org/plugins/insert-headers-and-footers/) instead.

Hope this helps.

Sure.

Here is my child theme’s functions.php:

<?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 third_party_tracking_code() { ?>

[script]

[script async src=ā€œhttps://www.googletagmanager.com/gtag/js?id=UA-101108062-3ā€>[script]

window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'UA-101108062-3'); [script] [script] <?php } add_action( 'wp_head', 'third_party_tracking_code' ); & Yes. If I can't get this fixed, I'll try the plugin out. PS: I put all of the scripts into brackets because my message wouldn't show up.

Hi,

Please change the entire code in your child theme’s functions.php file with this

<?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', '_returntrue' );

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

function third_party_tracking_code() { ?>

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-101108062-3"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-101108062-3');
</script>

<?php }

add_action( 'wp_head', 'third_party_tracking_code' );

Hope that helps

Thank you Paul it worked.

Can you explain what piece of code you changed? It seems odd that google analytics wouldn’t provide code that would work.

EDIT: There is this message on my website now:

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ā€˜_returntrue’ not found or invalid function name in /home2/andyvnguyen/yourcollegeblog.com/wp-includes/class-wp-hook.php on line 286

Here is a photo of the code:
http://puu.sh/BQO2X/5124c3fcd8.png

And here are lines 283 to 299

if ( $the_['accepted_args'] == 0 ) {
				$value = call_user_func_array( $the_['function'], array() );
			} elseif ( $the_['accepted_args'] >= $num_args ) {
				$value = call_user_func_array( $the_['function'], $args );
			} else {
				$value = call_user_func_array( $the_['function'], array_slice( $args, 0, (int)$the_['accepted_args'] ) );
			}
		}
	} while ( false !== next( $this->iterations[ $nesting_level ] ) );

	unset( $this->iterations[ $nesting_level ] );
	unset( $this->current_priority[ $nesting_level ] );

	$this->nesting_level--;

	return $value;
}

Hi Andy,

You can use a diff tool like KDiff to compare the changes of your code. From the above provide code, I see the script tag is inserted incorrectly [script] which @paul.r has corrected <script> with some other corrections.

I see that you have conflicts with other customizations that you have added. First I suggest you to remove your other customizations and test Google tracking code. If you’re still having issues, I would suggest you to use a plugin like Insert Headers and Footers (https://wordpress.org/plugins/insert-headers-and-footers/) to add your Analytics code.

Thanks!

Hi.

I tried removing the Google Tracking code completely and the
Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ā€˜_returntrue’ not found or invalid function name in /home2/andyvnguyen/yourcollegeblog.com/wp-includes/class-wp-hook.php on line 286

is still there.

Hi Andy,

Can you please check the PHP version running on your server and make sure to use PHP v5.6.x or later to avoid any incompatibility issues. Also check the WordPress version and make sure to use the latest one (https://codex.wordpress.org/Upgrading_WordPress). Also make sure to use latest version of X/Pro/Cornerstone (https://theme.co/changelog/).

If you’re still having issues, provide us with your FTP credentials in a secure note to check your child theme setup.

Thanks!

Hi.

Turns out the x.zip theme was corrupted. I had to redownload the theme and the child theme in order to get rid of it.

Glad you were able to resolve it :slight_smile: