the-seo-framework icon indicating copy to clipboard operation
the-seo-framework copied to clipboard

Adding Google Analytics to Head - option in SEO Framework

Open paaljoachim opened this issue 3 years ago • 3 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.

paaljoachim avatar Sep 17 '22 12:09 paaljoachim

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;
} );

sybrew avatar Sep 17 '22 20:09 sybrew

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.

paaljoachim avatar Sep 17 '22 21:09 paaljoachim

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 😄

sybrew avatar Sep 17 '22 22:09 sybrew

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

sybrew avatar Sep 25 '22 09:09 sybrew

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;
} );

paaljoachim avatar Sep 27 '22 13:09 paaljoachim

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.

sybrew avatar Sep 27 '22 15:09 sybrew

Thank you Sybre. I added a tutorial. https://easywebdesigntutorials.com/how-to-add-code-to-header-or-footer-in-wordpress/

paaljoachim avatar Sep 28 '22 09:09 paaljoachim