Hello There!

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

How to Implement Google Tag Manager in iOS Swift - Part 2 of 2

iOS with Swift

In the previous blog post, we saw how to create a GTM container, configure a tag and publish it. In this post, we are going to tackle the specifics of creating an iOS app and integrating it with GTM.

We won’t get into the details of how to create an iOS project in Xcode.Although if you have any queries or you’re facing a difficulty in doing so, please write to us and we’ll be able to help you out. Moving on, let us check out how to integrate Google Tag Manager pod in your project.

Adding GTM to Xcode Project

Close Xcode project, open terminal, navigate to your root directory of your project and write below command. This will create a podfile in your project.

Google Tag Manager

Open podfile and include Google Tag Manager pod in it.

iOS with Swift

Close podfile, and in terminal write below command to install the pod in your project.

mplement google tag manager in iOS

Great! Google Tag Manager is installed in your project. Now open your project in Xcode using xcworkspace file.

Adding a Bridging Header File

Create a bridging header file in your project. In Xcode, go to File -> New -> File, select Header File and create it within your target to keep it associated with your project files.

tag manager for iOS apps

Open the file in editor and import below mentioned headers in it.

app analytics

Add Objective-C bridging header to Build Settings. Go to Build Settings, select All and Combined and search for the bridging header. In the value field of key containing Objective-C Bridging Header, enter header filename Bridging-Header.h.

app analytics

Adding a Default GTM Container

Go to the directory where you downloaded GTM container, drag downloaded container and drop it into your Xcode project. One window will pop up, select ‘copy items if needed’ checkbox and add container to your project’s main target.

Google Tag Manager

Initializing GTM in Xcode Project

Open AppDelegate file and following changes in it.

Confirm to TAGContainerOpenerNotifier protocol.

iOS with Swift

Implement containerAvailable method.

iOS with Swift

In didFinishLaunchingWithOptions method, add below code.

implement google tag manager in iOS

GTM is initialized in your app now. Let’s fire a screen view datalayer.

Tracking a Screen Open Event

Go to your main screen’s view controller file, from viewDidAppear method fire a dataLayer to push a screen view as shown below.

tag manager for iOS apps

Viewing The Logs in Debug Area

In Xcode, go to Product -> Run to build and run your project. Once the build runs on iOS device or simulator, you will see openScreen event being pushed in Debug Area as shown below.

app analytics

Best Practices for Flawless Tracking
  • Create one constant for UA ID in GTM and use it as tracking ID for all Universal Analytics Tags instead of hardcoding UA ID value in each Tag.
  • It is best to push a screen view when screen actually appears to user hence push a screen view datalayer from viewDidAppear method.
  • Google Analytics is case sensitive. If any letter case changes while pushing values to datalayer, Google Analytics will consider it as a different value and in reports it will appear in a different row. So it is best to keep all values in any pre-defined cases and use those same to push into datalayer every time.
Concluding Thoughts

Installing GTM library in Xcode project and initializing it with adding a default container is all you need to ensure track screen views, events and other tracking available through GTM. While adding a GTM pod in Xcode project, it is preferred to add a library of version  ‘3.15.0’ to utilize datalayer functionality. If you have made any changes in the container, published it through GTM dashboard, then those changes will be reflected in the app within 12 hours of publishing that new container. It is not mandatory to include a container in app every time you publish a new version of it through GTM dashboard.

If you have any queries, comments or feedback for the post, please leave us a comment in the section below. Our team shall be sure to get back to you.

How to Implement Google Tag Manager in iOS Swift - Part 1 of 2

iOS with Swift

iOS with SwiftIn today’s era of mobile analytics, the implementation of the analytics with your mobile apps is extremely vital. From the analytics implementation survey carried out amongst a few enterprises, it is clear that they feel it’s very complicated to integrate analytics into their mobile app. Well, to make things simpler, Google has blessed us with their awesome tool that we all know as the Google Tag Manager. It’s a really compelling addition for the developers to integrate Google Analytics on any platform.

I am going publish a series of 2 blogs that will help you to integrate Google Tag Manager in iOS with Swift and get your data flowing in Google Analytics. In this blog post, we shall cover the basics for getting our tags ready and publishing container with Google Tag Manager.

Creating GTM Container

Create a Tag Manager account from tagmanger.google.com or use existing if you have created one already.

Under that account, create an iOS legacy container. Select container type iOS, SDK version Legacy iOS, give an appropriate name to the container and hit create button.

 

Google Tag ManagerYour container is now created! You can create tags, triggers and variables.

Let’s create a Screen View tag in the container - which should fire when any screen of the app opens.

Creating a ScreenView Tag

Go to Workspace -> Tags, and click New button.

mplement google tag manager in iOS

 

Now you will see two cards on your dashboard, Tag Configuration and Triggering. In Tag Configuration card you should set-up everything that is related to the tag that you are creating. Whereas for Triggering, you will create a triggering event on the occurrence of which above created tag will fire.

Select Tag Configuration card and select Universal Analytics tag type. Configure a tag as shown below.

tag manager for iOS apps

 

Select App View as track type since you are creating a screen view tag. Under Tracking ID field, set Google Analytics property ID. Click More Settings -> Fields to Set -> Add Field to add one key-value pair in the tag, a key will be the built-in variable called screenName and value will be {{screenName}} which is the datalayer variable that we shall cover as we move forward in this post.

Creating a Trigger

Now select the Triggering card and click ‘+’ icon from top right side of the layout and create a trigger as shown below.

 

app analyticsSelect Custom trigger type and configure this trigger to fire on some events when event name matches ‘openScreen’. Give appropriate name to trigger and save it. The “Trigger” is now added to your tag, give an appropriate name to tag and save it. This tag will fire from the app when datalayer event name matches ‘openScreen’.

Now create a datalayer variable, screenName, which you are using in your tag.

Creating a Variable

Go to Variables and select New under user defined variables section. You will see Variable Configuration card, select it and choose Data Layer Variable and configure variable as shown below.

 

Google Tag ManagerSpecify datalayer variable name as screenName using which this variable will be referred to. Check Set Default Value checkbox and set Default Value as NA so whenever this variable is used and its value is not defined then the default value will be pushed into the datalayer. Give appropriate name to this variable and save it.

The Screen View tag is configured perfectly, you can publish the container now.

Publishing the Container

Hit Submit button from the top right corner of the screen, a new layout will pop up. From submission configuration card, select Publish and Create Version and give a descriptive name to the version as shown below.

 

iOS with SwiftHit Publish button. That’s it! Your container is created.

Download the Container

You need to download a container as you shall be required to include it in your app as a default container. Go to Versions tab and click on Actions button for the container version which is Live and select Download option.

 

tag manager for iOS appsYour container published is with one Screen View tag and you have downloaded it now. You can include this container in your app as a default container.

Concluding Thoughts

Configuring an event or any other type of tags are also simple just like the process we described above. As a part of my closing thoughts, whenever you add, modify or delete something from the container, make sure you don’t forget to publish it, otherwise, the changes you have made will not reflect in your users’ app. As opposed to a website, in the case of an app, GTM takes a maximum of 12 hours to update the container in the app after publishing it. If your app is in the development stage, you can preview the container, or download and include it to update in your app. Before talking about updating the container, I shall take you through the process of integrating GTM SDK in the iOS app, in my upcoming blog post. Check out the second part of this blog series for getting started with Datalayer.

Bot Icon
Bot Icon

Tatvic Bot

Explore About Tatvic and Services