<commit_before>package org.stepic.droid.analytic

import android.content.Context
import android.content.res.Configuration
import android.content.res.Resources
import android.os.Bundle
import androidx.core.app.NotificationManagerCompat
import com.amplitude.api.Amplitude
import com.amplitude.api.Identify
import com.crashlytics.android.Crashlytics
import com.google.firebase.analytics.FirebaseAnalytics
import com.yandex.metrica.YandexMetrica
import org.json.JSONObject
import org.stepic.droid.base.App
import org.stepic.droid.configuration.Config
import org.stepic.droid.di.AppSingleton
import org.stepic.droid.util.isARSupported
import org.stepic.droid.util.isNightModeEnabled
import java.util.HashMap
import javax.inject.Inject

@AppSingleton
class AnalyticImpl
@Inject
constructor(
    context: Context,
    config: Config
) : Analytic {
    private val firebaseAnalytics: FirebaseAnalytics = FirebaseAnalytics.getInstance(context)
    private val amplitude = Amplitude.getInstance()
            .initialize(context, config.amplitudeApiKey)
            .enableForegroundTracking(App.application)

    init {
        amplitude.identify(Identify()
                .set(AmplitudeAnalytic.Properties.APPLICATION_ID, context.packageName)
                .set(AmplitudeAnalytic.Properties.PUSH_PERMISSION, if (NotificationManagerCompat.from(context).areNotificationsEnabled()) "granted" else "not_granted")
                .set(AmplitudeAnalytic.Properties.IS_NIGHT_MODE_ENABLED, context.isNightModeEnabled().toString())
                .set(AmplitudeAnalytic.Properties.IS_AR_SUPPORTED, context.isARSupported().toString())
        )
    }

    // Amplitude properties
    private fun syncAmplitudeProperties() {
        setScreenOrientation(Resources.getSystem().configuration.orientation)
    }

    override fun setUserId(userId: String) {
        firebaseAnalytics.setUserId(userId)
        Crashlytics.setUserIdentifier(userId)
        YandexMetrica.setUserProfileID(userId)
        amplitude.identify(Identify().set(AmplitudeAnalytic.Properties.STEPIK_ID, userId))
    }

    override fun setCoursesCount(coursesCount: Int) =
        amplitude.identify(Identify().set(AmplitudeAnalytic.Properties.COURSES_COUNT, coursesCount))

    override fun setSubmissionsCount(submissionsCount: Long) =
        amplitude.identify(Identify().set(AmplitudeAnalytic.Properties.SUBMISSIONS_COUNT, submissionsCount))

    override fun setScreenOrientation(orientation: Int) =
        amplitude.identify(Identify().set(AmplitudeAnalytic.Properties.SCREEN_ORIENTATION, if (orientation == Configuration.ORIENTATION_PORTRAIT) "portrait" else "landscape"))

    override fun setStreaksNotificationsEnabled(isEnabled: Boolean) =
        amplitude.identify(Identify().set(AmplitudeAnalytic.Properties.STREAKS_NOTIFICATIONS_ENABLED, if (isEnabled) "enabled" else "disabled"))

    override fun setTeachingCoursesCount(coursesCount: Int) =
        amplitude.identify(Identify().set(AmplitudeAnalytic.Properties.TEACHING_COURSES_COUNT, coursesCount))

    override fun reportAmplitudeEvent(eventName: String) = reportAmplitudeEvent(eventName, null)
    override fun reportAmplitudeEvent(eventName: String, params: MutableMap<String, Any>?) {
        syncAmplitudeProperties()
        val properties = JSONObject()
        params?.let {
            for ((k, v) in it.entries) {
                properties.put(k, v)
            }
        }
        amplitude.logEvent(eventName, properties)
        Crashlytics.log("$eventName=$params")
    }

    override fun setUserProperty(name: String, value: String) =
        amplitude.identify(Identify().set(name, value))
    // End of amplitude properties

    override fun reportEventValue(eventName: String, value: Long) {
        val bundle = Bundle()
        bundle.putLong(FirebaseAnalytics.Param.VALUE, value)
        reportEvent(eventName, bundle)
    }
<commit_msg>send amplitude analytics to appmetrica<commit_after>package org.stepic.droid.analytic

import android.content.Context
import android.content.res.Configuration
import android.content.res.Resources
import android.os.Bundle
import androidx.core.app.NotificationManagerCompat
import com.amplitude.api.Amplitude
import com.amplitude.api.Identify
import com.crashlytics.android.Crashlytics
import com.google.firebase.analytics.FirebaseAnalytics
import com.yandex.metrica.YandexMetrica
import org.json.JSONObject
import org.stepic.droid.base.App
import org.stepic.droid.configuration.Config
import org.stepic.droid.di.AppSingleton
import org.stepic.droid.util.isARSupported
import org.stepic.droid.util.isNightModeEnabled
import java.util.HashMap
import javax.inject.Inject

@AppSingleton
class AnalyticImpl
@Inject
constructor(
    context: Context,
    config: Config
) : Analytic {
    private val firebaseAnalytics: FirebaseAnalytics = FirebaseAnalytics.getInstance(context)
    private val amplitude = Amplitude.getInstance()
            .initialize(context, config.amplitudeApiKey)
            .enableForegroundTracking(App.application)

    init {
        amplitude.identify(Identify()
                .set(AmplitudeAnalytic.Properties.APPLICATION_ID, context.packageName)
                .set(AmplitudeAnalytic.Properties.PUSH_PERMISSION, if (NotificationManagerCompat.from(context).areNotificationsEnabled()) "granted" else "not_granted")
                .set(AmplitudeAnalytic.Properties.IS_NIGHT_MODE_ENABLED, context.isNightModeEnabled().toString())
                .set(AmplitudeAnalytic.Properties.IS_AR_SUPPORTED, context.isARSupported().toString())
        )
    }

    // Amplitude properties
    private fun syncAmplitudeProperties() {
        setScreenOrientation(Resources.getSystem().configuration.orientation)
    }

    override fun setUserId(userId: String) {
        firebaseAnalytics.setUserId(userId)
        Crashlytics.setUserIdentifier(userId)
        YandexMetrica.setUserProfileID(userId)
        amplitude.identify(Identify().set(AmplitudeAnalytic.Properties.STEPIK_ID, userId))
    }

    override fun setCoursesCount(coursesCount: Int) =
        amplitude.identify(Identify().set(AmplitudeAnalytic.Properties.COURSES_COUNT, coursesCount))

    override fun setSubmissionsCount(submissionsCount: Long) =
        amplitude.identify(Identify().set(AmplitudeAnalytic.Properties.SUBMISSIONS_COUNT, submissionsCount))

    override fun setScreenOrientation(orientation: Int) =
        amplitude.identify(Identify().set(AmplitudeAnalytic.Properties.SCREEN_ORIENTATION, if (orientation == Configuration.ORIENTATION_PORTRAIT) "portrait" else "landscape"))

    override fun setStreaksNotificationsEnabled(isEnabled: Boolean) =
        amplitude.identify(Identify().set(AmplitudeAnalytic.Properties.STREAKS_NOTIFICATIONS_ENABLED, if (isEnabled) "enabled" else "disabled"))

    override fun setTeachingCoursesCount(coursesCount: Int) =
        amplitude.identify(Identify().set(AmplitudeAnalytic.Properties.TEACHING_COURSES_COUNT, coursesCount))

    override fun reportAmplitudeEvent(eventName: String) = reportAmplitudeEvent(eventName, null)
    override fun reportAmplitudeEvent(eventName: String, params: MutableMap<String, Any>?) {
        syncAmplitudeProperties()
        val properties = JSONObject()
        params?.let {
            for ((k, v) in it.entries) {
                properties.put(k, v)
            }
        }
        amplitude.logEvent(eventName, properties)
        YandexMetrica.reportEvent(eventName, params)
        Crashlytics.log("$eventName=$params")
    }

    override fun setUserProperty(name: String, value: String) =
        amplitude.identify(Identify().set(name, value))
    // End of amplitude properties

    override fun reportEventValue(eventName: String, value: Long) {
        val bundle = Bundle()
        bundle.putLong(FirebaseAnalytics.Param.VALUE, value)
        reportEvent(eventName, bundle)
    }

{'fraction_non_alphanumeric': 0.09190427698574338, 'fraction_numerical': 0.0, 'mean_word_length': 4.175889328063241, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3106601', 'n_tokens_mistral': 2319, 'n_tokens_neox': 2250, 'n_words': 368}