Google analytics

Google analytics provides insights into who is using your site and what pages are being viewed. To use google analytics what I needed to do was to add a function into my functions.php called my_react_google_analytics. The —– is a placeholder for the tracking code provided by google analytics.

my_react_google_analytics

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

To use the function I then add an action for wp_head into my functions.php.

wp_head action

add_action('wp_head', 'my_react_google_analytics');

Comments

Leave a Reply

Your email address will not be published.