Integrating Google Tag Manager in a Flutter App

Optimise Your Flutter App with Google Tag Manager

by Ans Ali

Google Tag Manager (GTM) is a powerful tool that allows you to manage and deploy marketing tags (such as tracking pixels and code snippets) on your website or mobile app without requiring you to modify the codebase directly. By integrating GTM into your Flutter app, you can easily track user interactions, manage analytics, and set up custom events.

Why Use Google Tag Manager in Flutter?

GTM offers several advantages when integrated into a Flutter app:

Centralised Management

Manage all your tracking tags from a single interface.

Flexibility

Add or update tags without needing to release a new version of your app.

Analytics Integration

Seamlessly connect with Google Analytics, Firebase Analytics, and other marketing platforms.

Custom Events

Track user interactions such as button clicks, screen views, and more.

Prerequisites

Before you start, ensure you have:

Flutter SDK installed

Make sure your development environment is set up with Flutter.

A Google Tag Manager account

You'll need to create a GTM account and set up a container.

Firebase setup

Since GTM for mobile apps is typically implemented through Firebase, ensure your Flutter app is connected to Firebase.

Step 1: Set Up Google Tag Manager

Create a GTM Account and Container
  • Go to the Google Tag Manager website and sign in with your Google account.
  • Create a new account, name your container (e.g., "Flutter App"), and select "Mobile Apps" as the target platform.
  • Once the container is created, you'll receive a Container ID (e.g., GTM-XXXXXXX).
Add Tags in GTM
  • In your GTM workspace, you can add tags such as Google Analytics, Firebase events, or custom tags.
  • For example, to add a Google Analytics tag, select "Tag Configuration" and choose "Google Analytics: GA4 Configuration". Enter your Measurement ID, and set up the tag as required.

Step 2: Integrate Firebase into Your Flutter App

GTM for mobile apps works seamlessly with Firebase, so the next step is to set up Firebase in your Flutter project.

Add Firebase to Your Flutter App
  • If you haven't already, go to the Firebase Console, create a new project, and add your Flutter app to it.
  • Download the google-services.json file for Android and place it in the android/app directory of your Flutter project.
  • For iOS, download the GoogleService-Info.plist file and place it in the ios/Runner directory.
Add Firebase and GTM Dependencies
  • Open your pubspec.yaml file and add the necessary dependencies:

dependencies:
   firebase_core: latest_version
   firebase_analytics: ​latest_version

  • Run flutter pub get to install the dependencies.
Initialise Firebase
  • In your main.dart file, initialise Firebase before the runApp method.

import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';

void main() async {
   WidgetsFlutterBinding.ensureInitialized();
   await Firebase.initializeApp();
   runApp(MyApp());
}

Step 3: Configure Google Tag Manager in Your Flutter App

Enable Google Tag Manager in Firebase
  • Go back to the Firebase Console, navigate to your project, and select the "Analytics" section.
  • Under "Events," you'll find the option to link GTM to Firebase. Follow the prompts to link your GTM container to Firebase Analytics.
Set Up GTM in Your App
  • Add the GTM Container ID to your app. This is usually done by placing it in your Firebase configuration in the google-services.json or GoogleService-Info.plist files.
  • If you need to trigger specific GTM events from your Flutter code, you can use the firebase_analytics package to log events that are captured by GTM.

Step 4: Deploy and Test

Deploy Your App
  • Build and run your Flutter app on a device or emulator to ensure Firebase and GTM are integrated correctly.
  • GTM will start receiving data from your app via Firebase Analytics.
Test GTM Integration
  • Use Google Tag Assistant or Firebase DebugView to test the tags and events being fired from your app.
  • In the GTM interface, you can also preview and debug your container before publishing any changes.

Wrapping Up

  • Integrating Google Tag Manager into your Flutter app provides a flexible and powerful way to manage all your marketing and analytics tags from a single interface. By following the steps outlined in this guide, you can set up GTM to work with your Flutter app, allowing you to track user behaviour, manage events, and update tags without needing to modify your app's codebase directly.
  • With GTM, you can streamline your analytics and marketing efforts, ensuring that you have all the tools you need to understand and engage with your app's users effectively.

Stay Updated with Our Blog

Follow our blog for more insights and updates on mobile app development and other technology trends.

How to Build a Responsive Flutter App