Background

We're back for the fourth part of our five part series. This article will give you the tools you need to easily distribute the Google Tag Manager containers you create using Google Apps Script to your customers. If you haven't read parts one, two and three, you can check them out by following the links.

Introducing the MailApp service

Google Apps Script includes built-in email libraries to allow you to send emails. These can be used alongside HTML to send fully branded emails, but this blog will just cover the basics.

So let's go back and revisit how we made a container, using our configuration object and the TagManager service.

 var params = {
     		'gtmAccountId': '1234567',
            'customerName': 'Billy Bob',
            'analyticsAccountId': '1242535',
            'websiteUrl': 'https://billybob.co.nz',
            'callTrackingId': '1234567', //optional
            'analyticsPropertyId': 'UA-123456', //optional
            'customerEmail': 'billy@bob.com' //added this
}

 var path = 'accounts/' + params.gtmAccountId;

        var container = call(function() {
            return TagManager.Accounts.Containers.create({
                    'name': params.customerName,
                    'usageContext': ['WEB']
                },
                path)
        });

        var containerPath = container.path;

        var containerId = container.accountId;

I've also added a customer email address to our params object so we have somewhere to send the containers . To send the emails you can use the MailApp service or GmailApp service. Using GmailApp will provide better deliverability, as it is using your actual Gmail account to send the emails. You may need to use MailApp if you do not have a custom domain on your gmail account. The syntax for the sendEmail() method is the same for both.

 GmailApp.sendEmail(paramsHardcoded.customerEmail, 'Some subject', 'Some body');

This example would send an email to billy@bob.com, with the subject 'Some subject' and the body of 'Some body'.

Applying this to our containers

We can now send our previously created Google Tag Manager container to our customers using this service. You're going to need to escape the html in the container snippet – but luckily I've already done this for you.

We're also going to need to use the htmlBody option within the sendEmail() method so the snippet appears nicely in the email.

var gtmId = containerId;
  
var headTag = "&lt;!-- Google Tag Manager --&gt;<br>\
&lt;script&gt;(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':<br>\
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],<br>\
j=d.createElement(s),dl=l!='dataLayer'?'&amp;l='+l:'';j.async=true;j.src=<br>\
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);<br>\
})(window,document,'script','dataLayer','" + gtmId + "');&lt;/script&gt;<br>\
&lt;!-- End Google Tag Manager --&gt;"
 
var bodyTag = "<code>&lt;!-- Google Tag Manager (noscript) --&gt;<br>\
&lt;noscript&gt;&lt;iframe src=&quot;https://www.googletagmanager.com/ns.html?id=" + gtmId + "&quot;<br>\
height=&quot;0&quot; width=&quot;0&quot; style=&quot;display:none;visibility:hidden&quot;&gt;&lt;/iframe&gt;&lt;/noscript&gt;<br>\
&lt;!-- End Google Tag Manager (noscript) --&gt;</code>"

  var emailBody1 = "<p>Your Google Tag Manager container is now ready to install.\n\nPlease insert the following code just after the opening head tag of your website:</p><code>" + headTag;
  
  var emailBody2 = "</code>\n\n<p>Please insert the following code just after the opening body tag:</p>\n\n" + bodyTag;

  var options = {'htmlBody':emailBody1+emailBody2}
  
  GmailApp.sendEmail(paramsHardcoded.customerEmail,"Your Google Tag Manager Container","This email contains your tracking code for online marketing, please open it on an email client that supports HTML.", options);

This should come through looking like this:

Screenshot of HTML email of container snippet

Seeing as it's using HTML, you can customise your email template to look however you want. It's totally up to you.

Final thoughts

I would recommend adding a bit more content to your emails explaining the purpose of Google Tag Manager, particularly if this is going to clients without any knowledge of it.

I've found agencies generally prefer to use something like Marketo to send their containers – but this is a great option for those who don't have access to that sort of software.

Our final part in the series will cover some strategies for checking the installation status of your containers – as this whole process is pointless unless the containers are implemented. Until next time, have fun!

 


Curtis Warner

Curtis is one of the Co-Founders of Leadtosale. He comes from a digital marketing background and has extensive experience with Google Analytics and Google Tag Manager.

Leave a Reply

Your email address will not be published. Required fields are marked *