@harrison.goodwin
Setting up Google Tag Manager in Swift involves the following steps:
- Sign up for a Google Tag Manager account and create a new container.
- Generate a container snippet by going to your container's "Install Google Tag Manager" page.
- In your Xcode project, navigate to the AppDelegate.swift file.
- Import the GoogleTagManager library by adding the following line at the top of the AppDelegate.swift file:
import GoogleTagManager
- Inside the application(_:didFinishLaunchingWithOptions:) method, add the following code to initialize the Google Tag Manager:
TAGManager.instance().logger.setLogLevel(kTAGLoggerLogLevelVerbose)
TAGManager.instance().logger.setDebugDispatchURL(true)
TAGManager.instance().logger.setDebugDispatchData(true)
TAGContainerOpener.openContainer(withId: "GTM-XXXXXX", // Replace "GTM-XXXXXX" with your container ID
tagManager: TAGManager.instance(),
openType: kTAGOpenTypePreferFresh,
timeout: nil,
notifier: self)
- Implement the TAGContainerOpenerNotifier protocol in the AppDelegate class to receive container load notifications.
class AppDelegate: UIResponder, UIApplicationDelegate, TAGContainerOpenerNotifier {
// ...
func containerAvailable(_ container: TAGContainer!) {
container.refresh()
}
}
- In your desired view controller, import GoogleTagManager library by adding the following line at the top:
import GoogleTagManager
- Add the following code to obtain the container reference and push any desired data layer changes:
let container = TAGManager.instance().container(withId: "GTM-XXXXXX") // Replace "GTM-XXXXXX" with your container ID
container?.push(["event": "xyz", "key1": "value1"]) // Example data layer update
- Build and run your app to test the integration of Google Tag Manager in Swift.
Note: The above steps provide a basic setup for Google Tag Manager. Further customization can be done using the GTM SDK documentation and API references according to your specific requirements.