Google Analytics without Soil

Hi all,

Can we still add our Google Analytics tracking code in Sage like we used to do in Roots?
What is the recommended way? I am not using the Soil plugin.

Regards,
Steven

You have a couple options:

  • Use Soil
  • Get the code that was in Roots before. It’s all in the git history
  • Add Google Analytics according to their website
  • Use any of the multitude of WP plugins that add GA
2 Likes

Grab the code from HTML5 Boilerplate and stick it in your base.php.

If you want a repeatable way to do it in your own sage fork or something where you only need to include the GA UA ID, go the old Roots route: wrap that HTML5BP <script> code in a php function, make the “UA-XXXXX-X” bit a PHP CONSTANT you set somewhere and attach it to wp_footer

 //in some file, even wp-config.php
 define('GOOGLE_ID', 'UA-123456-7');

 //and in config.php or setup.php
 function includeGA(){ ?>
     //<script> from boilerplate here
<?php } ?>
add_action('wp_footer', currentNAMESPACE .\'includeGA', 10);

I’ve taken to using CMB2 to make a site options form within the admin. I include a field for it there and pull that in.

1 Like