question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Google tag manager not working on IOS

See original GitHub issue

The configuration for the tag manager was done by adding something like the following to the index.html of the project:

<!-- Global Site Tag (gtag.js) - Google AdWords: GOOGLE_CONVERSION_ID -->
<script async src="https://www.googletagmanager.com/gtag/js?id=AW-GOOGLE_CONVERSION_ID"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'AW-GOOGLE_CONVERSION_ID');
</script>

Debugging the issue the apparently reason that it isn’t working is because when an event is triggered, the app makes a GET request to https://www.google-analytics.com/collect , but on the iOS build inspected through the Safari develop console, that request is never made.

Also, when opening the page from the Safari browser it’s working as expected.

The versions are:

@capacitor/core” : “^1.0.0-beta.17”

@capacitor/ios”: “^1.0.0-beta.19”

@capacitor/cli”: “^1.0.0-beta.19”

Testing from an iPhone 6 running IOS 11.4.1

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:45 (4 by maintainers)

github_iconTop GitHub Comments

11reactions
tyeon95commented, Jun 15, 2020

@Snake231088 is it possible to implement a similar fix for a GTM script?

I’ve managed to relay the ga settings from GTM by doing the following:

  1. Create a new GTM variable called Empty function as a Custom Javascript type with the following contents:
function() {
     return function() {}
}
  1. Create a new GTM variable called JS - GetClientId as a Custom Javascript type with the following contents:
function() {
     if (window.Storage) {
       return window.localStorage.getItem('_clientId') || undefined;
     }
     return;
}
  1. Create a new GTM variable called JS - SetClientId as a Custom Javascript type with the following contents:
function() {
     return function() {
       if (window.Storage) {
          window.localStorage.setItem('_clientId', ga.getAll()[0].get('clientId'));
       }
     }
}
  1. Open your GTM variable where you set the Google Analytics Settings type. Go to More Settings > Fields to Set.
    1. Set Field Name to checkProtocolTask with Value as {{Empty function}}
    2. Set Field Name to storage with Value as none
    3. Set Field Name to hitCallback with Value as {{JS - SetClientId}}
    4. Set Field Name to clientId with Value as {{JS - GetClientId}}
  2. Publish, and you’re done!

Essentially, 4.i - removes the requirement for the URI protocol to be restricted to http or https. 4.ii, 4.iii, 4.iv - moves the GA client id to be stored on localStorage instead of in the cookie.

7reactions
Snake231088commented, May 20, 2020

Ok i’ve fixed

// cookies -> localStorage
var GA_LOCAL_STORAGE_KEY = 'ga:clientId';
ga('create', 'XXXXXXXXX', {
        'storage': 'none',
        'clientId': localStorage.getItem(GA_LOCAL_STORAGE_KEY)
});
ga(function (tracker) {
        localStorage.setItem(GA_LOCAL_STORAGE_KEY, tracker.get('clientId'));
});

// allow capacitor:// as protocol
ga('set', 'checkProtocolTask', function () { /* nothing */ });
Read more comments on GitHub >

github_iconTop Results From Across the Web

Google Tag Manager for iOS
Sign in to your Tag Manager account. · Click ADMIN in the top navigation bar. · On the CONTAINER column, click the drop-down...
Read more >
Google tag manger for ios not loading container - Stack Overflow
I am trying to add google tag manager in my xcode project which will track firebase events in ios. I've created a tag...
Read more >
GTM for iOS: Can't load default container - Issue Tracker
We were using the export function in Google Tag Manager to get the default container. The saved exported file was for some reason...
Read more >
Google Tag Manager for iOS - Braze
This article covers how to initialize, configure, and implement the Google Tag manager into your iOS app.
Read more >
Set up and install Tag Manager - Google Support
4. Verify that your tags work · Click the Preview button. · Enter the website URL on which you installed the Tag Manager...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found