sybrew/the-seo-framework

Adding Google Analytics to Head - option in SEO Framework

Closed this issue · 7 comments

I am currently redoing a how to add Google Analytics to a WordPress web site tutorial.
Lets see if I got this correct...
It seems that a part of this process is adding tracking code into the header.php of a site.

Which means how would a person go about doing so in a simple way?
One can probably add a plugin to where one can add code to the header or footer of a site, or manually add the code to header.php.

What if we instead could just add the code into SEO Framework? In an option someplace.


I am not sure if I am on or off track with this whole issue.... Thanks.

Hello!

We have an Extension for that, but I'm going to sunset it in favor of the (poorly written and performance-impeding) Google Sitekit in the future. The reason is that Google is letting us all down by sunsetting Universal Analytics.

Until then, see https://theseoframework.com/extensions/cord/.

If you really want custom snippets in the header, this piece of code goes a long way:

add_action( 'wp_head', function() {
    echo <<<'SNIPPET'

<my entire>

<snippet goes="here!">

SNIPPET;
} );

Hey @sybrew

That would be something like is mentioned here:https://kinsta.com/knowledgebase/add-code-wordpress-header-footer/

/* Describe what the code snippet does so you can remember later on */
add_action('wp_head', 'your_function_name');
function your_function_name(){
?>
PASTE HEADER CODE HERE
<?php
};

Then using a code snippet plugin.

That's correct, that is like what I proposed, but with a different writing style -- mine is focused on performance, while theirs is made easier for starting programmers. Pick your poison 😄

Closing as resolved (we already have this feature). Don't hesitate to ask for other features!

Hey @sybrew

You mentioned:
"That's correct, that is like what I proposed, but with a different writing style -- mine is focused on performance, while theirs is made easier for starting programmers."

The code (This is the Google tag I am going to use for my tutorial site.)

/* Describe what the code snippet does so you can remember later on */
add_action('wp_head', 'your_function_name');
function your_function_name(){
?>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-N82M0N2BYX"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-N82M0N2BYX');
</script>
<?php
};

How would you set it up using?

add_action( 'wp_head', function() {
    echo <<<'SNIPPET'

<my entire>

<snippet goes="here!">

SNIPPET;
} );

Hi Paal,

Check out https://www.php.net/manual/en/language.types.string.php, it explains my example snippet as "Nowdoc".

add_action( 'wp_head', function() {
    echo <<<'HTML'
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-N82M0N2BYX"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-N82M0N2BYX');
</script>
HTML;
} );

I used HTML instead of SNIPPET, because some parsers (mainly, VScode) read that and adjust syntax highlighting accordingly. But, you can use any word you'd like.

Note the lack of PHP opening and closing tags. I try to avoid them because even when they're inside functions that never run, they'll slow down the entire PHP script.