<commit_before>//
//  AnalyticsManager.swift
//  Rocket.Chat
//
//  Created by <NAME> on 27/06/18.
//  Copyright © 2018 Rocket.Chat. All rights reserved.
//

import Foundation
import Firebase
import Crashlytics

enum AnalyticsProvider {
    case fabric
    case firebase
}

enum Event {
    case signup
    case login
    case screenView(screenName: String)
    case messageSent(subscriptionType: String, server: String)
    case mediaUpload(mediaType: String, subscriptionType: String)
    case reaction(subscriptionType: String)
    case serverSwitch(server: String, serverCount: Int)
    case updatedSubscriptionSorting(sorting: String, grouping: String)
    case updatedWebBrowser(browser: String)
    case updatedTheme(theme: String)

}

enum UserProperty {
    case server(server: String)

    var propertyName: String {
        switch self {
        case .server:
            return "Server"
        }
    }
}

struct AnalyticsManager {
    static func set(userProperty: UserProperty) {
        // Make sure the user has opted in for sending his usage data
        guard !AnalyticsCoordinator.isUsageDataLoggingDisabled else {
            return
        }

        switch userProperty {
        case let .server(server):
            Analytics.setUserProperty(server, forName: userProperty.propertyName)
        }
    }

    static func log(event: Event) {
        // Make sure the user has opted in for sending his usage data
        guard !AnalyticsCoordinator.isUsageDataLoggingDisabled else {
            return
        }

        // Don't log screen views when using firebase since it already logs them automatically
        if event.name() != Event.screenView(screenName: "").name() {
            Analytics.logEvent(
                event.name(for: .firebase),
                parameters: event.parameters(for: .firebase)
            )
        }

        // Fabric has a specific method for logging login and sign up events
        if event.name() == Event.login.name() {
            Answers.logLogin(
                withMethod: nil,
                success: 1,
                customAttributes: event.parameters(for: .fabric)
            )

            return
        }

        if event.name() == Event.signup.name() {
            Answers.logSignUp(
                withMethod: nil,
                success: 1,
                customAttributes: event.parameters(for: .fabric)
            )

            return
        }

        Answers.logCustomEvent(
            withName: event.name(for: .fabric),
            customAttributes: event.parameters(for: .fabric)
        )
    }
}

extension Event {
    func name(for provider: AnalyticsProvider? = nil) -> String {
        switch self {
        case .signup:
            return provider == .firebase ? AnalyticsEventSignUp : "sign_up"
        case .login:
            return provider == .firebase ? AnalyticsEventLogin : "login"
        case .screenView: return "screen_view"
        case .messageSent: return "message_sent"
        case .mediaUpload: return "media_upload"<commit_msg>Create new events related to the New Workspace button & status changes<commit_after>//
//  AnalyticsManager.swift
//  Rocket.Chat
//
//  Created by <NAME> on 27/06/18.
//  Copyright © 2018 Rocket.Chat. All rights reserved.
//

import Foundation
import Firebase
import Crashlytics

enum AnalyticsProvider {
    case fabric
    case firebase
}

enum Event {
    case newWorkspace
    case signup
    case login
    case updateStatus
    case screenView(screenName: String)
    case messageSent(subscriptionType: String, server: String)
    case mediaUpload(mediaType: String, subscriptionType: String)
    case reaction(subscriptionType: String)
    case serverSwitch(server: String, serverCount: Int)
    case updatedSubscriptionSorting(sorting: String, grouping: String)
    case updatedWebBrowser(browser: String)
    case updatedTheme(theme: String)
}

enum UserProperty {
    case server(server: String)

    var propertyName: String {
        switch self {
        case .server:
            return "Server"
        }
    }
}

struct AnalyticsManager {
    static func set(userProperty: UserProperty) {
        // Make sure the user has opted in for sending his usage data
        guard !AnalyticsCoordinator.isUsageDataLoggingDisabled else {
            return
        }

        switch userProperty {
        case let .server(server):
            Analytics.setUserProperty(server, forName: userProperty.propertyName)
        }
    }

    static func log(event: Event) {
        // Make sure the user has opted in for sending his usage data
        guard !AnalyticsCoordinator.isUsageDataLoggingDisabled else {
            return
        }

        // Don't log screen views when using firebase since it already logs them automatically
        if event.name() != Event.screenView(screenName: "").name() {
            Analytics.logEvent(
                event.name(for: .firebase),
                parameters: event.parameters(for: .firebase)
            )
        }

        // Fabric has a specific method for logging login and sign up events
        if event.name() == Event.login.name() {
            Answers.logLogin(
                withMethod: nil,
                success: 1,
                customAttributes: event.parameters(for: .fabric)
            )

            return
        }

        if event.name() == Event.signup.name() {
            Answers.logSignUp(
                withMethod: nil,
                success: 1,
                customAttributes: event.parameters(for: .fabric)
            )

            return
        }

        Answers.logCustomEvent(
            withName: event.name(for: .fabric),
            customAttributes: event.parameters(for: .fabric)
        )
    }
}

extension Event {
    func name(for provider: AnalyticsProvider? = nil) -> String {
        switch self {
        case .signup:
            return provider == .firebase ? AnalyticsEventSignUp : "sign_up"
        case .login:
            return provider == .firebase ? AnalyticsEventLogin : "login"
        case .newWorkspace: return "new_workspace"
        case .updateStatus: return "status_update"
        case .screenView: return "screen_view"
        case .messageSent: return "message_sent"
        case .mediaUpload: return "media_upload"
{'fraction_non_alphanumeric': 0.08203744842907014, 'fraction_numerical': 0.003808314820691844, 'mean_word_length': 2.0791402051783097, 'pattern_counts': {'":': 0, '<': 5, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1549937', 'n_tokens_mistral': 1699, 'n_tokens_neox': 1616, 'n_words': 483}