capacitor: Google tag manager not working on IOS
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
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 45 (4 by maintainers)
I’ve managed to relay the
gasettings from GTM by doing the following:Empty functionas aCustom Javascripttype with the following contents:JS - GetClientIdas aCustom Javascripttype with the following contents:JS - SetClientIdas aCustom Javascripttype with the following contents:Google Analytics Settingstype. Go toMore Settings > Fields to Set.Field NametocheckProtocolTaskwithValueas{{Empty function}}Field NametostoragewithValueasnoneField NametohitCallbackwithValueas{{JS - SetClientId}}Field NametoclientIdwithValueas{{JS - GetClientId}}Essentially,
4.i- removes the requirement for the URI protocol to be restricted tohttporhttps.4.ii,4.iii,4.iv- moves the GA client id to be stored on localStorage instead of in the cookie.Ok i’ve fixed
Update: I’ve found out there are two reasons why it fails:
capacitor://protocol is not allowed, in analytics thecheckProtocolTasksetting can be overriddenstoragesetting tononeand using localstorage to store the client id helps.You dont need Firebase for Google Tag Manager to receive events. Building an Ionic Angular iOS App - Everything works on Web and Android. This worked for me! Placed right after the TagManager script gtag(‘set’, {‘checkProtocolTask’: function(){ /* nothing */ } });
Credit to @ericgopak for showing this. Not using Firebase.