diff --git a/vector/build.gradle b/vector/build.gradle index fb1199a497..d786ca9389 100644 --- a/vector/build.gradle +++ b/vector/build.gradle @@ -233,11 +233,6 @@ android { // Set to true if you want to enable strict mode in debug buildConfigField "boolean", "ENABLE_STRICT_MODE_LOGS", "false" - // Analytics. Set to empty strings to just disable analytics - buildConfigField "String", "ANALYTICS_POSTHOG_HOST", "\"https://posthog-poc.lab.element.dev\"" - buildConfigField "String", "ANALYTICS_POSTHOG_API_KEY", "\"rs-pJjsYJTuAkXJfhaMmPUNBhWliDyTKLOOxike6ck8\"" - buildConfigField "String", "ANALYTICS_POLICY_URL", "\"https://element.io/cookie-policy\"" - signingConfig signingConfigs.debug } @@ -248,11 +243,6 @@ android { buildConfigField "boolean", "LOW_PRIVACY_LOG_ENABLE", "false" buildConfigField "boolean", "ENABLE_STRICT_MODE_LOGS", "false" - // Analytics. Set to empty strings to just disable analytics - buildConfigField "String", "ANALYTICS_POSTHOG_HOST", "\"https://posthog.hss.element.io\"" - buildConfigField "String", "ANALYTICS_POSTHOG_API_KEY", "\"phc_Jzsm6DTm6V2705zeU5dcNvQDlonOR68XvX2sh1sEOHO\"" - buildConfigField "String", "ANALYTICS_POLICY_URL", "\"https://element.io/cookie-policy\"" - postprocessing { removeUnusedCode true removeUnusedResources true diff --git a/vector/src/debug/java/im/vector/app/config/AnalyticsConfig.kt b/vector/src/debug/java/im/vector/app/config/AnalyticsConfig.kt new file mode 100644 index 0000000000..34f2d4f92b --- /dev/null +++ b/vector/src/debug/java/im/vector/app/config/AnalyticsConfig.kt @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2021 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.config + +import im.vector.app.BuildConfig +import im.vector.app.features.analytics.AnalyticsConfig + +val analyticsConfig: AnalyticsConfig = object : AnalyticsConfig { + override val isEnabled = BuildConfig.APPLICATION_ID == "im.vector.app.debug" + override val postHogHost = "https://posthog-poc.lab.element.dev" + override val postHogApiKey = "rs-pJjsYJTuAkXJfhaMmPUNBhWliDyTKLOOxike6ck8" + override val policyLink = "https://element.io/cookie-policy" +} diff --git a/vector/src/main/java/im/vector/app/features/analytics/AnalyticsConfig.kt b/vector/src/main/java/im/vector/app/features/analytics/AnalyticsConfig.kt index a9045dc325..46119f5563 100644 --- a/vector/src/main/java/im/vector/app/features/analytics/AnalyticsConfig.kt +++ b/vector/src/main/java/im/vector/app/features/analytics/AnalyticsConfig.kt @@ -16,36 +16,9 @@ package im.vector.app.features.analytics -import im.vector.app.BuildConfig -import im.vector.app.features.analytics.log.analyticsTag -import timber.log.Timber - -data class AnalyticsConfig( - val postHogHost: String, - val postHogApiKey: String -) { - companion object { - /** - * Read the analytics config from the Build config - */ - fun getConfig(): AnalyticsConfig? { - val postHogHost = BuildConfig.ANALYTICS_POSTHOG_HOST.takeIf { it.isNotEmpty() } - ?: return null.also { - Timber.tag(analyticsTag.value).w("Analytics is disabled, ANALYTICS_POSTHOG_HOST is empty") - } - val postHogApiKey = BuildConfig.ANALYTICS_POSTHOG_API_KEY.takeIf { it.isNotEmpty() } - ?: return null.also { - Timber.tag(analyticsTag.value).w("Analytics is disabled, ANALYTICS_POSTHOG_API_KEY is empty") - } - - return AnalyticsConfig( - postHogHost = postHogHost, - postHogApiKey = postHogApiKey - ) - } - - fun isAnalyticsEnabled(): Boolean { - return getConfig() != null - } - } +interface AnalyticsConfig { + val isEnabled: Boolean + val postHogHost: String + val postHogApiKey: String + val policyLink: String } diff --git a/vector/src/main/java/im/vector/app/features/analytics/impl/DefaultVectorAnalytics.kt b/vector/src/main/java/im/vector/app/features/analytics/impl/DefaultVectorAnalytics.kt index 997bea88a5..eaf2e42705 100644 --- a/vector/src/main/java/im/vector/app/features/analytics/impl/DefaultVectorAnalytics.kt +++ b/vector/src/main/java/im/vector/app/features/analytics/impl/DefaultVectorAnalytics.kt @@ -20,11 +20,11 @@ import android.content.Context import com.posthog.android.PostHog import com.posthog.android.Properties import im.vector.app.BuildConfig -import im.vector.app.features.analytics.AnalyticsConfig +import im.vector.app.config.analyticsConfig import im.vector.app.features.analytics.VectorAnalytics -import im.vector.app.features.analytics.log.analyticsTag import im.vector.app.features.analytics.itf.VectorAnalyticsEvent import im.vector.app.features.analytics.itf.VectorAnalyticsScreen +import im.vector.app.features.analytics.log.analyticsTag import im.vector.app.features.analytics.store.AnalyticsStore import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.flow.Flow @@ -123,10 +123,12 @@ class DefaultVectorAnalytics @Inject constructor( private fun createAnalyticsClient() { Timber.tag(analyticsTag.value).d("createAnalyticsClient()") - val config: AnalyticsConfig = AnalyticsConfig.getConfig() - ?: return Unit.also { Timber.tag(analyticsTag.value).w("Analytics is disabled") } + if (analyticsConfig.isEnabled.not()) { + Timber.tag(analyticsTag.value).w("Analytics is disabled") + return + } - posthog = PostHog.Builder(context, config.postHogApiKey, config.postHogHost) + posthog = PostHog.Builder(context, analyticsConfig.postHogApiKey, analyticsConfig.postHogHost) // Record certain application events automatically! (off/false by default) // .captureApplicationLifecycleEvents() // Record screen views automatically! (off/false by default) diff --git a/vector/src/main/java/im/vector/app/features/analytics/ui/consent/AnalyticsOptInFragment.kt b/vector/src/main/java/im/vector/app/features/analytics/ui/consent/AnalyticsOptInFragment.kt index 0360221a69..35bc25e3af 100644 --- a/vector/src/main/java/im/vector/app/features/analytics/ui/consent/AnalyticsOptInFragment.kt +++ b/vector/src/main/java/im/vector/app/features/analytics/ui/consent/AnalyticsOptInFragment.kt @@ -21,8 +21,8 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import com.airbnb.mvrx.activityViewModel -import im.vector.app.BuildConfig import im.vector.app.R +import im.vector.app.config.analyticsConfig import im.vector.app.core.extensions.setTextWithColoredPart import im.vector.app.core.platform.VectorBaseFragment import im.vector.app.core.utils.openUrlInChromeCustomTab @@ -60,7 +60,7 @@ class AnalyticsOptInFragment @Inject constructor( fullTextRes = R.string.analytics_opt_in_content, coloredTextRes = R.string.analytics_opt_in_content_link, onClick = { - openUrlInChromeCustomTab(requireContext(), null, BuildConfig.ANALYTICS_POLICY_URL) + openUrlInChromeCustomTab(requireContext(), null, analyticsConfig.policyLink) } ) } diff --git a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsSecurityPrivacyFragment.kt b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsSecurityPrivacyFragment.kt index f3a054d46d..2dd1e93c4a 100644 --- a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsSecurityPrivacyFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsSecurityPrivacyFragment.kt @@ -35,6 +35,7 @@ import androidx.recyclerview.widget.RecyclerView import com.airbnb.mvrx.fragmentViewModel import com.google.android.material.dialog.MaterialAlertDialogBuilder import im.vector.app.R +import im.vector.app.config.analyticsConfig import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.dialogs.ExportKeysDialog import im.vector.app.core.extensions.queryExportKeys @@ -50,7 +51,6 @@ import im.vector.app.core.utils.copyToClipboard import im.vector.app.core.utils.openFileSelection import im.vector.app.core.utils.toast import im.vector.app.databinding.DialogImportE2eKeysBinding -import im.vector.app.features.analytics.AnalyticsConfig import im.vector.app.features.analytics.ui.consent.AnalyticsConsentViewActions import im.vector.app.features.analytics.ui.consent.AnalyticsConsentViewModel import im.vector.app.features.analytics.ui.consent.AnalyticsConsentViewState @@ -294,7 +294,7 @@ class VectorSettingsSecurityPrivacyFragment @Inject constructor( } private fun setUpAnalytics() { - analyticsCategory.isVisible = AnalyticsConfig.isAnalyticsEnabled() + analyticsCategory.isVisible = analyticsConfig.isEnabled analyticsConsent.setOnPreferenceClickListener { analyticsConsentViewModel.handle(AnalyticsConsentViewActions.SetUserConsent(analyticsConsent.isChecked)) diff --git a/vector/src/release/java/im/vector/app/config/AnalyticsConfig.kt b/vector/src/release/java/im/vector/app/config/AnalyticsConfig.kt new file mode 100644 index 0000000000..7f7ef1a54e --- /dev/null +++ b/vector/src/release/java/im/vector/app/config/AnalyticsConfig.kt @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2021 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.config + +import im.vector.app.BuildConfig +import im.vector.app.features.analytics.AnalyticsConfig + +val analyticsConfig: AnalyticsConfig = object : AnalyticsConfig { + override val isEnabled = BuildConfig.APPLICATION_ID == "im.vector.app" + override val postHogHost = "https://posthog.hss.element.io" + override val postHogApiKey = "phc_Jzsm6DTm6V2705zeU5dcNvQDlonOR68XvX2sh1sEOHO" + override val policyLink = "https://element.io/cookie-policy" +}