GTM integration on iOS requires installing our native SDK, and then setting up GTM to trigger SDK functions via a custom function handler class that bridges GTM and our native SDKs. This demo apps is a proof of concept, with just the 3 most important functionalities implemented (logging events, setting user Id, and setting user properties) in the AmplitudeGTMHandler class.
Note, if you are running this demo app locally, you will need to install Cocoapods and run pod install
in the base directory, otherwise you will need to install our SDK manually by following the Setup instructions steps 2-4.
Here is our GTM setup:
- eventProperties
- eventType
- userId
- userProperties
- logEvent: Event equals 'logEvent'
- setUserId: Event equals 'setUserId'
- setUserProperties: Event equals 'setUserProperties'
logEvent
- type: Function Call
- Function Name: logEvent
- Fire on trigger: Event equals 'logEvent'
- arguments: eventType = {{eventType}}, eventProperties = {{eventProperties}}
setUserId
- type: Function Call
- Function Name: setUserId
- Fire on trigger: Event equals 'setUserId'
- arguments: userId = {{userId}}
setUserProperties
- type: Function Call
- Function Name: setUserProperties
- Fire on trigger: Event equals 'setUserProperties'
- arguments: userProperties = {{userProperties}}
The idea is that when you want to log an event, you push a "logEvent" event to the data layer with the variables for eventType and eventProperties -> activates the logEvent trigger -> activates the logEvent tag -> calls the function logEvent in the AmplitudeGTMHandler -> calls our SDK's logEvent.
GTM iOS Demo: https://github.com/amplitude/GTM-iOS-Demo
- AmplitudeGTMHandler: https://github.com/amplitude/GTM-iOS-Demo/blob/master/m2048/AmplitudeGTMHandler.m
- Setting up the handlers: https://github.com/amplitude/GTM-iOS-Demo/blob/master/m2048/M2AppDelegate.m#L20-L34
- Example of setting userId and userProperties: https://github.com/amplitude/GTM-iOS-Demo/blob/master/m2048/Controller/M2ViewController.m#L40-L43
- Example of logging an event: https://github.com/amplitude/GTM-iOS-Demo/blob/master/m2048/State/M2GameManager.m#L208-L209
A lot of the other functionality from the SDK hasn't been implemented (such as logging revenue, user property operations, etc), but this proof of concept demonstrates that GTM integration with Amplitude is possible, and events are being logged with the correct user Ids. Implementing the other functionalities would follow a similar pattern.