RELATED TO: RealHomes Theme

Google Tag Manager (GTM) lets you manage website tags without the need to edit your code. Google Tag Manager is a free tag management solution you can use to add and update your site tags to understand its conversions and analytics better.

You can add GTM to the RealHomes theme in the following easy steps:

Step 1: Create Google Tag Manager Account

Please create a free Google Tag Manager account by following the instructions here.

Step 2: Add a Google Tag Manager code snippet to the RealHomes theme. 

We will have to customize the functions.php file of the theme to add the Google Tag Manager code to avoid any extra plugin installation. It’s recommended to use the child theme for changes to functions.php. If you don’t, your edits will simply be wiped out the next time your theme updates. A pre-built ready-to-use child theme is included in the main theme pack which you have downloaded from ThemeForest. It is commonly called rh-main-package.zip.

Below is the sample that you can put into functions.php. Make sure to replace the function block code with your own GTM code snippet.

  add_action('wp_head', function () {
    if (!is_admin() && !current_user_can('administrator')) {
?>
        <!-- Google Tag Manager -->
        <script>
            (function(w, d, s, l, i) {
                w[l] = w[l] || [];
                w[l].push({
                    'gtm.start': new Date().getTime(),
                    event: 'gtm.js'
                });
                var f = d.getElementsByTagName(s)[0],
                    j = d.createElement(s),
                    dl = l != 'dataLayer' ? '&l=' + l : '';
                j.async = true;
                j.src ='https://www.googletagmanager.com/gtm.js?id=' + i + dl;
                f.parentNode.insertBefore(j, f);
            })(window, document, 'script', 'dataLayer', 'GTM-XXXXXXX');
        </script>
        <!-- End Google Tag Manager -->
    <?php
    }
}, 0);

add_action('wp_body_open', function () {
    if (!is_admin() && !current_user_can('administrator')) {
    ?>
        <!-- Google Tag Manager (noscript) -->
        <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXXXX" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
        <!-- End Google Tag Manager (noscript) -->
<?php
    }
}, 1);
?>

Please sign up at our support center and create a support ticket for any further assistance, and our support team will be happy to assist you.