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

iOS with SwiftIn 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.

The following two tabs change content below.
Sameer Sanghavi

Sameer Sanghavi

Sameer is a Partner & VP of Customer Development at Tatvic Analytics, heading the operations for the company in North America. He has been working across a global footprint, in creating strategies & building a synergistic portfolio of businesses for the firm, whose philosophy is to help enterprises make better data-driven decisions. Sameer’s background has been in running successful technology businesses and leading large initiatives.
, , , ,
Previous Post
How Tatvic’s PredictN Solution Can Help You Increase Your Remarketing ROI
Next Post
Video Performance Tracking: Know the Level of User Engagement Through Videos

2 Comments. Leave new

  • Hi
    thanks information
    it is very useful to me

    Reply
  • Dear Mr.Burley,

    I have integrate Firebase 4.2.0 sdk to my IOS Objective -c project, and it’s working fine and shows the logs event. After creating google tag manager account with a Firebase IOS container, I added the container to container folder and add google tag manager sdk to the project (not sure if it’s the right sdk or which version of GTM sdk I should use) Is there a specific version of GTM sdk should be use with Firebase .

    The container has been publish and set to the latest version.

    When I run the app in simulator only Firebase logs are showing. I think there is something missing and I don’t want to use cocapod.

    Please help and does the container need a tag to start showing it’s log?

    Regards

    Reply

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu