Server-side tagging for mobile apps

You can configure your mobile apps using the Google Analytics for Firebase SDK to work with server-side tagging in Google Tag Manager.

Server-side tagging lets you stage, redact, and augment your data before sending it onward to destination products like Google Analytics. Learn more about Server-side tagging.

To set up server-side tagging for mobile apps:

  1. Get your app ready for server-side tagging
  2. Configure your server container to support mobile traffic
  3. Enable server-side tagging in your GA4 property

Prerequisites

Step 1: Get your app ready for server-side tagging

iOS

  1. Enable the server-side tagging upload feature and set up the custom URL scheme for server-side tagging debug mode in the Info.plist file. Replace the BUNDLE_ID on the following with the app's bundle identifier.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
    ...
            <key>CFBundleURLTypes</key>
            <array>
                    <dict>
                            <key>CFBundleTypeRole</key>
                            <string>None</string>
                            <key>CFBundleURLName</key>
                            <string>BUNDLE_ID</string>
                            <key>CFBundleURLSchemes</key>
                            <array>
                                    <string>tagmanager.sgtm.c.BUNDLE_ID</string>
                            </array>
                    </dict>
            </array>
            <key>GOOGLE_ANALYTICS_SGTM_UPLOAD_ENABLED</key>
            <true/>
    </dict>
    </plist>
    
  2. To enable the debug or preview mode, add a call to Analytics.handleOpen(_:) in a lifecycle method to pass the launch URL.

    SwiftUI

    @main
    struct MyApplication: App {
      var body: some Scene {
        WindowGroup {
          ContentView()
            .onOpenURL { url in
              Analytics.handleOpen(url)
            }
        }
      }
    }
    

    UIScene

    class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession,
            options connectionOptions: UIScene.ConnectionOptions) {
    ...
        if let urlContext = connectionOptions.urlContexts.first {
            let url = urlContext.url
            Analytics.handleOpen(url)
        }
      }
    }
    

    For apps that don't use SwiftUI or UIScene, no action is required. SDK receives the launch URL from the ApplicationDidFinishLaunchingNotification.

Android

  1. Install the latest release of the Google Analytics for Firebase SDK.
  2. Enable sGTM by adding the following to the application's AndroidManifest.xml file.

    <!-- Preview Activity to enable preview mode –->
      <activity
          android:name="com.google.firebase.analytics.GoogleAnalyticsServerPreviewActivity"
          android:exported="true"
          android:noHistory="true" >
        <intent-filter>
          <action android:name="android.intent.action.VIEW" />
          <category android:name="android.intent.category.DEFAULT" />
          <category android:name="android.intent.category.BROWSABLE" />
          <data android:scheme="tagmanager.sgtm.c.<APP_PACKAGE_ID>" />
        </intent-filter>
      </activity>
    
    <!-- Meta-data that enables uploads to sGTM –->
      <meta-data android:name="google_analytics_sgtm_upload_enabled" android:value="true" />
    

Step 2: Configure your server container to support mobile traffic

The following section assumes you have an existing server-side tagging deployment and are familiar with its concepts. If you're new to server-side tagging, you may take the server-side tagging fundamentals course.

Set up the Google Analytics: GA4 (App) client

A client is a Tag Manager resource type that intercepts certain types of incoming requests and generates events that can be passed to a destination, like Google Analytics 4. In this case, the HTTP request is a Google Analytics 4 event, so you need to configure the built-in Google Analytics: GA4 (App) client. You only need one GA4 client per data source such as GA SDK and website. Learn more about How clients work.

  1. In your server container, go to Clients and click New.
  2. Click Client Configuration, then select Google Analytics: GA4 (App).

    Image showing the Google Analytics: GA4 (App) client option in Tag Manager

  3. Enter a name for your client and click Save.

Configure your Google Analytics: GA4 tag

To send data to Google Analytics, you'll need to create a Google Analytics: GA4 tag in the server container. The client parses the incoming HTTP request into an event data object. The tag will take this event data object, map it to the correct format, and then send it to Google Analytics 4.

  1. In your server container, go to Tags and click New.
  2. Click Tag Configuration, then choose Google Analytics: GA4.

    Image showing the Google Analytics: GA4 tag settings in Tag Manager

By default, the tag will inherit all the relevant fields and parameters from the event data object created by the client:

  • If the incoming Google Analytics 4 request passes through the Google Analytics: GA4 (App) client, the tag inherits the App ID and event parameters.
  • If the incoming Google Analytics 4 requests passes through the Google Analytics: GA4 (Web) client, the tag inherits the Measurement ID and event parameters.

Set up triggers for mobile traffic

You should then establish when the GA4 tag should fire. The Google Analytics: GA4 (App) client parses the incoming request into an event data object, and the tag will inherit its values. Therefore, you will typically want the tag to fire whenever the GA4 client claims the request.

To set up the triggers:

  1. In the tag settings, click Triggering.
  2. To create a new trigger, click the + icon in the top right corner.
  3. Click Trigger Configuration to choose the trigger type.
  4. Choose Custom or Custom Event from the list.

    • The Custom trigger type, by default, fires the tag when any event is generated by a server-side client.
    • The Custom Event trigger type can be used to fire the tag on a specific event name or an event that matches a regular expression pattern.
  5. To enable the list of activation conditions for this trigger such as to fire only on mobile or web events data, select Some Events.

    1. You'll see Event Name pre-selected. Click that selector and select Choose Built-In Variable from the list.
    2. To automatically return the name of the client that generated the event, select Client Name.
    3. Use the name of the client you configured earlier to set the condition accordingly. Your trigger should look like this:

      Image showing the Trigger Configuration screen after creating a trigger in Tag Manager

  6. Save the trigger and enter a descriptive name. For example, you can use "All GA4 App Events."

  7. You should now be back in the tag settings with the new trigger in its place. Save the tag and enter a descriptive name, for example "GA4." Your tag should look like this:

    Image showing the GA4 tag after setting up a trigger

Preview your container

You can use Google Tag Manager to preview and debug your server container before publishing the changes. Note that previewing won't work until you've enabled server-side tagging in your GA4 property.

To have your app requests trigger the preview configuration, you need to:

  1. Select Preview while in your Google Tag Manager server container.
  2. Click the More Actions icon on the top right and select Send requests from an app.
  3. Enter your app ID and click Generate QR Code.

    Image showing a QR code when the Generate QR Code is clicked

  4. Scan the QR code with your phone's camera app. Proceed to interact with your app as you normally would to generate preview events.

  5. Click OK to close the dialog.

Publish your container

After you've verified your changes, publish the container to make those changes live for all traffic. Learn more about Publishing, versions, and approvals.

Step 3: Enable server-side tagging in your GA4 property

  1. In your Google Analytics 4 property, go to Admin.
  2. Open Data streams.
  3. Select your application's stream configuration from the iOS or Android tab.

    Image showing the Data streams page in GA4

  4. Click Configure SDK settings, then Configure server-side Tag Manager.

    Image showing the Configure server-side Tag Manager option in GA4

  5. To enable the SDK to send measurement data to the server container URL:

    1. Toggle the Send data to a server-side Tag Manager container.
    2. Copy the server container URL. You can find this in your Google Tag Manager by going to Admin > Container Settings > Server container URLs.
    3. Select the percentage of traffic you want to route to the server container. You may choose an option under 100% to validate your server setup and configuration before using server-side tagging for all of your traffic.

    Image showing the GA4's Configuration page

  6. Click Save. The SDK will retrieve the updated server container URL and direct measurement data to it.

Known limitations

  • For Android applications:

    • To avoid potential security vulnerabilities involving server containers and Google Play services, events will go through a stateless proxy server in between the SDK and your server container. This proxy will validate the source and endpoint of the request, but it won't inspect or store any data about the events. Consistent with how GA4 collects data from EU-based devices, the proxy will be in the EU for EU-based traffic.
    • Automatically logged in-app purchases rely on integration with Google Play backend and won't be sent to the server container.
    • app_remove event won't be reported on Android.
  • Connecting your app data streams in Google Analytics to your Google Ads account is still necessary to ensure that your SDK data and conversions are imported into Google Ads. Server-side tagging does not offer any inherent integration between your SDK and Google Ads.

API

You can use the Tag Manager REST API to access the Tag Manager configuration data. For more information about the API: