Google Analytics

Google Analytics is an analytics product. You can track Rover events into it using the following sample integration. You may need to customize it to get the best value out of the tool.


Mapping Rover Events to Google Analytics Events

After integrating both the Rover and Google Analytics SDKs into your project, your goal will be to send the Rover events to Google Analytics as suitable GA events.


Experience Presented

Use a Google Analytics Screen View.

NotificationCenter.default.addObserver(forName: ExperienceViewController.experiencePresentedNotification, object: nil, queue: nil) { notification in
    let experience = notification.userInfo?[ExperienceViewController.experienceUserInfoKey] as! Experience
    
    var attributes: [String: String] = [
        "experienceID": experience.id,
        "experienceName": experience.name
    ]
    
    if let campaignID = notification.userInfo?[ExperienceViewController.campaignIDUserInfoKey] as? String {
        attributes["campaignID"] = campaignID
    }
    
    guard let tracker = GAI.sharedInstance().defaultTracker else { return }
    tracker.set(kGAIScreenName, value: "Rover Experience")
    
    guard let builder = GAIDictionaryBuilder.createScreenView() else { return }
    builder.setAll(attributes)
    tracker.send(builder.build() as [NSObject : AnyObject])
}

Experience Dismissed

Use a generic Google Analytics event:

NotificationCenter.default.addObserver(forName: ExperienceViewController.experienceDismissedNotification, object: nil, queue: nil) { notification in
    let experience = notification.userInfo?[ExperienceViewController.experienceUserInfoKey] as! Experience
    
    var attributes: [String: String] = [
        "experienceID": experience.id,
        "experienceName": experience.name
    ]
    
    if let campaignID = notification.userInfo?[ExperienceViewController.campaignIDUserInfoKey] as? String {
        attributes["campaignID"] = campaignID
    }
    
    guard let tracker = GAI.sharedInstance().defaultTracker else { return }
    tracker.set(kGAIScreenName, value: "Rover Experience")
    guard let builder = GAIDictionaryBuilder.createEvent(withCategory: "Rover Experience", action: "Dismissed", label: experience.name, value: nil) else { return }
    builder.setAll(attributes)
    tracker.send(builder.build() as [NSObject : AnyObject])
}

Experience Viewed

This event describes the session (duration, primarily) spent viewing a Rover Experience. Google Analytics’ built-in session tracking does not give you a way to distinguish between separate experiences, so we’ll manually track session information computed by Rover itself instead.

NotificationCenter.default.addObserver(forName: ExperienceViewController.experienceViewedNotification, object: nil, queue: nil) { notification in
    let experience = notification.userInfo?[ExperienceViewController.experienceUserInfoKey] as! Experience
    let duration = notification.userInfo?[ExperienceViewController.durationUserInfoKey] as! Double
    
    var attributes: [String: String] = [
        "experienceID": experience.id,
        "experienceName": experience.name,
        "duration": String(describing: duration)
    ]
    
    if let campaignID = notification.userInfo?[ExperienceViewController.campaignIDUserInfoKey] as? String {
        attributes["campaignID"] = campaignID
    }
    
    guard let tracker = GAI.sharedInstance().defaultTracker else { return }
    tracker.set(kGAIScreenName, value: "Rover Experience")
    guard let builder = GAIDictionaryBuilder.createEvent(withCategory: "Rover Experience", action: "Viewed", label: experience.name, value: nil) else { return }
    builder.setAll(attributes)
    tracker.send(builder.build() as [NSObject : AnyObject])
}

Screen Presented

Use a Google Analytics Screen View. This will also start a Google Analytics session for the experience screen.

NotificationCenter.default.addObserver(forName: ScreenViewController.screenPresentedNotification, object: nil, queue: nil) { notification in
    let experience = notification.userInfo?[ScreenViewController.experienceUserInfoKey] as! Experience
    let screen = notification.userInfo?[ScreenViewController.screenUserInfoKey] as! Screen
    
    var attributes: [String: String] = [
        "experienceID": experience.id,
        "experienceName": experience.name,
        "screenID": screen.id,
        "screenName": screen.name
    ]
    
    if let campaignID = notification.userInfo?[ScreenViewController.campaignIDUserInfoKey] as? String {
        attributes["campaignID"] = campaignID
    }
    
    guard let tracker = GAI.sharedInstance().defaultTracker else { return }
    tracker.set(kGAIScreenName, value: "Rover Experience Screen")
    
    guard let builder = GAIDictionaryBuilder.createScreenView() else { return }
    builder.setAll(attributes)
    tracker.send(builder.build() as [NSObject : AnyObject])
}

Screen Dismissed

Use a generic Google Analytics event:

NotificationCenter.default.addObserver(forName: ScreenViewController.screenDismissedNotification, object: nil, queue: nil) { notification in
    let experience = notification.userInfo?[ScreenViewController.experienceUserInfoKey] as! Experience
    let screen = notification.userInfo?[ScreenViewController.screenUserInfoKey] as! Screen
    
    var attributes: [String: String] = [
        "experienceID": experience.id,
        "experienceName": experience.name,
        "screenID": screen.id,
        "screenName": screen.name
    ]
    
    if let campaignID = notification.userInfo?[ScreenViewController.campaignIDUserInfoKey] as? String {
        attributes["campaignID"] = campaignID
    }
    
    guard let tracker = GAI.sharedInstance().defaultTracker else { return }
    tracker.set(kGAIScreenName, value: "Rover Experience Screen")
    guard let builder = GAIDictionaryBuilder.createEvent(withCategory: "Rover Experience Screen", action: "Dismissed", label: experience.name, value: nil) else { return }
    builder.setAll(attributes)
    tracker.send(builder.build() as [NSObject : AnyObject])
}

Screen Viewed

This event describes the session (duration, primarily) spent viewing a Rover Experience. Google Analytics’ built-in session tracking does not give you a way to distinguish between separate experiences, so we’ll manually track session information computed by Rover itself instead.

NotificationCenter.default.addObserver(forName: ScreenViewController.screenViewedNotification, object: nil, queue: nil) { notification in
    let experience = notification.userInfo?[ScreenViewController.experienceUserInfoKey] as! Experience
    let screen = notification.userInfo?[ScreenViewController.screenUserInfoKey] as! Screen
    let duration = notification.userInfo?[ScreenViewController.durationUserInfoKey] as! Double
    
    var attributes: [String: String] = [
        "experienceID": experience.id,
        "experienceName": experience.name,
        "screenID": screen.id,
        "screenName": screen.name,
        "duration": String(describing: duration)
    ]
    
    if let campaignID = notification.userInfo?[ScreenViewController.campaignIDUserInfoKey] as? String {
        attributes["campaignID"] = campaignID
    }
    
    guard let tracker = GAI.sharedInstance().defaultTracker else { return }
    tracker.set(kGAIScreenName, value: "Rover Experience Screen")
    guard let builder = GAIDictionaryBuilder.createEvent(withCategory: "Rover Experience Screen", action: "Viewed", label: experience.name, value: nil) else { return }
    builder.setAll(attributes)
    tracker.send(builder.build() as [NSObject : AnyObject])
}

Block Tapped

Use a Google Analytics event:

NotificationCenter.default.addObserver(forName: ScreenViewController.blockTappedNotification, object: nil, queue: nil) { notification in
    let experience = notification.userInfo?[ScreenViewController.experienceUserInfoKey] as! Experience
    let screen = notification.userInfo?[ScreenViewController.screenUserInfoKey] as! Screen
    let block = notification.userInfo?[ScreenViewController.blockUserInfoKey] as! Block
    
    var attributes: [String: String] = [
        "experienceID": experience.id,
        "experienceName": experience.name,
        "screenID": screen.id,
        "screenName": screen.name,
        "blockID": block.id,
        "blockName": block.name,
    ]
    
    if let campaignID = notification.userInfo?[ScreenViewController.campaignIDUserInfoKey] as? String {
        attributes["campaignID"] = campaignID
    }
    
    guard let tracker = GAI.sharedInstance().defaultTracker else { return }
    tracker.set(kGAIScreenName, value: "Rover Experience Screen")
    guard let builder = GAIDictionaryBuilder.createEvent(withCategory: "Rover Experience", action: "Block Tapped", label: block.name, value: nil) else { return }
    builder.setAll(attributes)
    tracker.send(builder.build() as [NSObject : AnyObject])
}