<commit_before>//
//  AutomaticEvents.swift
//  Mixpanel
//
//  Created by <NAME> on 3/8/17.
//  Copyright © 2017 Mixpanel. All rights reserved.
//

protocol AEDelegate {
    func track(event: String?, properties: Properties?)
    func setOnce(properties: Properties)
    func increment(property: String, by: Double)
    #if DECIDE
        func trackPushNotification(_ userInfo: [AnyHashable: Any], event: String)
    #endif
}

#if DECIDE || TV_AUTO_EVENTS
import Foundation
import UIKit
import StoreKit
import UserNotifications

class AutomaticEvents: NSObject, SKPaymentTransactionObserver, SKProductsRequestDelegate {

    var _minimumSessionDuration: UInt64 = 10000
    var minimumSessionDuration: UInt64 {
        set {
            _minimumSessionDuration = newValue
        }
        get {
            return _minimumSessionDuration
        }
    }
    var _maximumSessionDuration: UInt64 = UINT64_MAX
    var maximumSessionDuration: UInt64 {
        set {
            _maximumSessionDuration = newValue
        }
        get {
            return _maximumSessionDuration
        }
    }
    var awaitingTransactions = [String: SKPaymentTransaction]()
    let defaults = UserDefaults(suiteName: "Mixpanel")
    var delegate: AEDelegate?
    var sessionLength: TimeInterval = 0
    var sessionStartTime: TimeInterval = Date().timeIntervalSince1970
    var hasAddedObserver = false
    var automaticPushTracking = true
    var firstAppOpen = false

    func initializeEvents() {
        let firstOpenKey = "MPFirstOpen"
        if let defaults = defaults, !defaults.bool(forKey: firstOpenKey) {
            if !isExistingUser() {
                firstAppOpen = true
            }
            defaults.set(true, forKey: firstOpenKey)
            defaults.synchronize()
        }
        if let defaults = defaults, let infoDict = Bundle.main.infoDictionary {
            let appVersionKey = "MPAppVersion"
            let appVersionValue = infoDict["CFBundleShortVersionString"]
            let savedVersionValue = defaults.string(forKey: appVersionKey)
            if let appVersionValue = appVersionValue as? String,
                let savedVersionValue = savedVersionValue,
                appVersionValue.compare(savedVersionValue, options: .numeric, range: nil, locale: nil) == .orderedDescending {
                delegate?.track(event: "$ae_updated", properties: ["$ae_updated_version": appVersionValue])
                defaults.set(appVersionValue, forKey: appVersionKey)
                defaults.synchronize()
            } else if savedVersionValue == nil {
                defaults.set(appVersionValue, forKey: appVersionKey)
                defaults.synchronize()
            }
        }

        NotificationCenter.default.addObserver(self,
                                               selector: #selector(appWillResignActive(_:)),
                                               name: UIApplication.willResignActiveNotification,
                                               object: nil)

        NotificationCenter.default.addObserver(self,
                                               selector: #selector(appDidBecomeActive(_:)),
                                               name: UIApplication.didBecomeActiveNotification,
                                               object: nil)

        #if DECIDE
        SKPaymentQueue.default().add(self)
        
        DispatchQueue.main.async { [weak self] in
            guard let self = self else { return }

            self.setupAutomaticPushTracking()
        }
        #endif
    }

    @objc func appWillResignActive(_ notification: Notification) {
        sessionLength = roundOneDigit(num: Date().timeIntervalSince1970 - sessionStartTime)
        if sessionLength >= Double(minimumSessionDuration / 1000) &&
           sessionLength <= Double(maximumSessionDuration / 1000) {
            delegate?.track(event: "$ae_session", properties: ["$ae_session_length": sessionLength])
            delegate?.increment(property: "$ae_total_app_sessions", by: 1)
            delegate?.increment(property: "$ae_total_app_session_length", by: sessionLength)
        }
    }

    @objc private func appDidBecomeActive(_ notification: Notification) {
        sessionStartTime = Date().timeIntervalSince1970
        if firstAppOpen {<commit_msg>Added check for automaticPushTracking<commit_after>//
//  AutomaticEvents.swift
//  Mixpanel
//
//  Created by <NAME> on 3/8/17.
//  Copyright © 2017 Mixpanel. All rights reserved.
//

protocol AEDelegate {
    func track(event: String?, properties: Properties?)
    func setOnce(properties: Properties)
    func increment(property: String, by: Double)
    #if DECIDE
        func trackPushNotification(_ userInfo: [AnyHashable: Any], event: String)
    #endif
}

#if DECIDE || TV_AUTO_EVENTS
import Foundation
import UIKit
import StoreKit
import UserNotifications

class AutomaticEvents: NSObject, SKPaymentTransactionObserver, SKProductsRequestDelegate {

    var _minimumSessionDuration: UInt64 = 10000
    var minimumSessionDuration: UInt64 {
        set {
            _minimumSessionDuration = newValue
        }
        get {
            return _minimumSessionDuration
        }
    }
    var _maximumSessionDuration: UInt64 = UINT64_MAX
    var maximumSessionDuration: UInt64 {
        set {
            _maximumSessionDuration = newValue
        }
        get {
            return _maximumSessionDuration
        }
    }
    var awaitingTransactions = [String: SKPaymentTransaction]()
    let defaults = UserDefaults(suiteName: "Mixpanel")
    var delegate: AEDelegate?
    var sessionLength: TimeInterval = 0
    var sessionStartTime: TimeInterval = Date().timeIntervalSince1970
    var hasAddedObserver = false
    var automaticPushTracking = true
    var firstAppOpen = false

    func initializeEvents() {
        let firstOpenKey = "MPFirstOpen"
        if let defaults = defaults, !defaults.bool(forKey: firstOpenKey) {
            if !isExistingUser() {
                firstAppOpen = true
            }
            defaults.set(true, forKey: firstOpenKey)
            defaults.synchronize()
        }
        if let defaults = defaults, let infoDict = Bundle.main.infoDictionary {
            let appVersionKey = "MPAppVersion"
            let appVersionValue = infoDict["CFBundleShortVersionString"]
            let savedVersionValue = defaults.string(forKey: appVersionKey)
            if let appVersionValue = appVersionValue as? String,
                let savedVersionValue = savedVersionValue,
                appVersionValue.compare(savedVersionValue, options: .numeric, range: nil, locale: nil) == .orderedDescending {
                delegate?.track(event: "$ae_updated", properties: ["$ae_updated_version": appVersionValue])
                defaults.set(appVersionValue, forKey: appVersionKey)
                defaults.synchronize()
            } else if savedVersionValue == nil {
                defaults.set(appVersionValue, forKey: appVersionKey)
                defaults.synchronize()
            }
        }

        NotificationCenter.default.addObserver(self,
                                               selector: #selector(appWillResignActive(_:)),
                                               name: UIApplication.willResignActiveNotification,
                                               object: nil)

        NotificationCenter.default.addObserver(self,
                                               selector: #selector(appDidBecomeActive(_:)),
                                               name: UIApplication.didBecomeActiveNotification,
                                               object: nil)

        #if DECIDE
        SKPaymentQueue.default().add(self)
        
        DispatchQueue.main.async { [weak self] in
            guard let self = self else { return }
            if self.automaticPushTracking {
                self.setupAutomaticPushTracking()
            }
        }
        #endif
    }

    @objc func appWillResignActive(_ notification: Notification) {
        sessionLength = roundOneDigit(num: Date().timeIntervalSince1970 - sessionStartTime)
        if sessionLength >= Double(minimumSessionDuration / 1000) &&
           sessionLength <= Double(maximumSessionDuration / 1000) {
            delegate?.track(event: "$ae_session", properties: ["$ae_session_length": sessionLength])
            delegate?.increment(property: "$ae_total_app_sessions", by: 1)
            delegate?.increment(property: "$ae_total_app_session_length", by: sessionLength)
        }
    }

    @objc private func appDidBecomeActive(_ notification: Notification) {
        sessionStartTime = Date().timeIntervalSince1970
        if firstAppOpen {
{'fraction_non_alphanumeric': 0.07800114876507754, 'fraction_numerical': 0.010338885697874785, 'mean_word_length': 2.172740524781341, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2243206', 'n_tokens_mistral': 2227, 'n_tokens_neox': 2081, 'n_words': 501}