How to add google analytics to iOS app

How to add google analytics to iOS app
October 12, 2019 No Comments Development Pushpendra Kumar

Google analytics for iOS app

There are multiple ways of implementing google analytics in your ios application. The main question is How you can implement google analytics in your iOS development. This tutorial will take you through the basic implementation of the Google Analytics SDK via Firebase. Analytics uses CocoaPods to install and manage dependencies. In other words, Let’s start. Open a terminal window and navigate to the location of the Xcode project for your application. If you have not already created a Podfile for your application, create one now. If you do not have already pod install in your Xcode project then follow this link – https://developers.google.com/ios/guides/cocoapods

  • Open you terminal and reached to your project location via cd command.
  • After that, run pod init command.
  • Now run pod install command.
  • open you file and code like as given below.
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

target 'GA-Example' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  # Pods for GA-Example
  
  pod 'GoogleAnalytics'
  pod 'Firebase/Core'

  target 'GA-ExampleTests' do
    inherit! :search_paths
    # Pods for testing
  end

  target 'GA-ExampleUITests' do
    # Pods for testing
  end

end

Therefore, hit on Command + B, For building the project. After that, building successfully please open your AppDelegate.swift file and import the firebase SDK and also write a line of code into your AppDelegate.swift file. For instance –

//
//  AppDelegate.swift
//  GA-Example
//
//  Created by Pushpendra on 11/10/19.
//  Copyright © 2019 Pushpendra. All rights reserved.
//

import UIKit
import Firebase

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        FirebaseApp.configure()
        return true
    }

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }
}

After that, If you want to track via scene view then add the below code. For instance –

import UIKit
import Firebase
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        Analytics.setScreenName("Scene Name", screenClass: "Class Name")
    }
}

In Conclusion, I hope you understand this completely how analytics does work for iOS application. So thanks for visiting on my website. I hope this tutorial did help you. below you can find below important links!

Tags
About The Author
Pushpendra Kumar I am passionate about mobile application development and professional developer at Colour Moon Technologies Pvt Ltd (www.thecolourmoon.com). This website I have made so that I can meet with new challenges and can share here.

Leave a reply

Your email address will not be published. Required fields are marked *