Skip to content

Commit

Permalink
Merge pull request #15664 from brave/wallet-p3a-trx-sent
Browse files Browse the repository at this point in the history
Add weekly Wallet transaction sent P3A metric
  • Loading branch information
DJAndries authored Oct 28, 2022
2 parents 947fbf5 + 5325b26 commit be63639
Show file tree
Hide file tree
Showing 23 changed files with 207 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public void onConnectionError(MojoException e) {
if (mTxService != null) mTxService.close();
if (mEthTxManagerProxy != null) mEthTxManagerProxy.close();
if (mSolanaTxManagerProxy != null) mSolanaTxManagerProxy.close();
if (mBraveWalletP3A != null) mBraveWalletP3A.close();
if (mBraveWalletService != null) mBraveWalletService.close();

mKeyringService = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.google.android.material.tabs.TabLayout;

import org.chromium.base.CommandLine;
import org.chromium.base.Log;
import org.chromium.brave_wallet.mojom.AccountInfo;
import org.chromium.brave_wallet.mojom.AssetRatioService;
import org.chromium.brave_wallet.mojom.BlockchainRegistry;
import org.chromium.brave_wallet.mojom.BlockchainToken;
import org.chromium.brave_wallet.mojom.BraveWalletConstants;
import org.chromium.brave_wallet.mojom.BraveWalletP3a;
import org.chromium.brave_wallet.mojom.BraveWalletService;
import org.chromium.brave_wallet.mojom.CoinType;
import org.chromium.brave_wallet.mojom.JsonRpcService;
import org.chromium.brave_wallet.mojom.KeyringService;
import org.chromium.brave_wallet.mojom.NetworkInfo;
Expand All @@ -54,6 +58,7 @@
import org.chromium.chrome.browser.crypto_wallet.util.TokenUtils;
import org.chromium.chrome.browser.crypto_wallet.util.TransactionUtils;
import org.chromium.chrome.browser.crypto_wallet.util.Utils;
import org.chromium.chrome.browser.crypto_wallet.util.WalletConstants;
import org.chromium.chrome.browser.util.TabUtils;
import org.chromium.url.GURL;

Expand Down Expand Up @@ -169,6 +174,15 @@ private KeyringService getKeyringService() {
return null;
}

private BraveWalletP3a getBraveWalletP3A() {
Activity activity = getActivity();
if (activity instanceof BraveWalletBaseActivity) {
return ((BraveWalletBaseActivity) activity).getBraveWalletP3A();
}

return null;
}

@Override
public void show(FragmentManager manager, String tag) {
try {
Expand Down Expand Up @@ -443,6 +457,7 @@ private void approveTransaction() {
"approveTransaction", error.getProviderError(), errorMessage);
return;
}
reportTransactionForP3A();
mApproved = true;
if (mTransactionConfirmationListener != null) {
mTransactionConfirmationListener.onApproveTransaction();
Expand All @@ -451,6 +466,25 @@ private void approveTransaction() {
});
}

private void reportTransactionForP3A() {
if (!WalletConstants.SEND_TRANSACTION_TYPES.contains(mTxInfo.txType)
&& !(mCoinType == CoinType.FIL && mTxInfo.txType == TransactionType.OTHER)) {
return;
}
JsonRpcService jsonRpcService = getJsonRpcService();
BraveWalletP3a braveWalletP3A = getBraveWalletP3A();
assert jsonRpcService != null && braveWalletP3A != null;

jsonRpcService.getNetwork(mCoinType, selectedNetwork -> {
boolean countTestNetworks = CommandLine.getInstance().hasSwitch(
BraveWalletConstants.P3A_COUNT_TEST_NETWORKS_SWITCH);
if (countTestNetworks
|| !WalletConstants.KNOWN_TEST_CHAIN_IDS.contains(selectedNetwork.chainId)) {
braveWalletP3A.reportTransactionSent(mCoinType, true);
}
});
}

private boolean canUpdateUi() {
return isAdded() && getActivity() != null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
import org.chromium.brave_wallet.mojom.ProviderError;
import org.chromium.brave_wallet.mojom.TransactionInfo;
import org.chromium.brave_wallet.mojom.TransactionStatus;
import org.chromium.brave_wallet.mojom.TransactionType;
import org.chromium.brave_wallet.mojom.TxData;
import org.chromium.brave_wallet.mojom.TxService;
import org.chromium.chrome.R;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,13 @@ public final class WalletConstants {
public static final String SOL_INS_BPF = "BPFLoaderUpgradeab1e11111111111111111111111";
public static final String SOL_INS_SIG_VERIFY = "Ed25519SigVerify111111111111111111111111111";
public static final String SOL_INS_SECP = "KeccakSecp256k11111111111111111111111111111";

public static final List<String> KNOWN_TEST_CHAIN_IDS = Arrays.asList(
BraveWalletConstants.GOERLI_CHAIN_ID, BraveWalletConstants.SEPOLIA_CHAIN_ID,
BraveWalletConstants.LOCALHOST_CHAIN_ID, BraveWalletConstants.SOLANA_TESTNET,
BraveWalletConstants.SOLANA_DEVNET, BraveWalletConstants.FILECOIN_TESTNET);
public static final List<Integer> SEND_TRANSACTION_TYPES = Arrays.asList(
TransactionType.ETH_SEND, TransactionType.ERC20_TRANSFER,
TransactionType.SOLANA_SYSTEM_TRANSFER, TransactionType.SOLANA_SPL_TOKEN_TRANSFER,
TransactionType.SOLANA_SPL_TOKEN_TRANSFER_WITH_ASSOCIATED_TOKEN_ACCOUNT_CREATION);
}
30 changes: 29 additions & 1 deletion browser/brave_wallet/brave_wallet_p3a_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ namespace brave_wallet {

class BraveWalletP3AUnitTest : public testing::Test {
public:
BraveWalletP3AUnitTest() {
BraveWalletP3AUnitTest()
: task_environment_(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {
histogram_tester_ = std::make_unique<base::HistogramTester>();
}

Expand Down Expand Up @@ -142,4 +143,31 @@ TEST_F(BraveWalletP3AUnitTest, ReportOnboardingAction) {
1);
}

TEST_F(BraveWalletP3AUnitTest, TransactionSent) {
histogram_tester_->ExpectTotalCount(kEthTransactionSentHistogramName, 0);
histogram_tester_->ExpectTotalCount(kSolTransactionSentHistogramName, 0);
histogram_tester_->ExpectTotalCount(kFilTransactionSentHistogramName, 0);

BraveWalletP3A* wallet_p3a = wallet_service_->GetBraveWalletP3A();

wallet_p3a->ReportTransactionSent(mojom::CoinType::ETH, true);
histogram_tester_->ExpectUniqueSample(kEthTransactionSentHistogramName, 1, 1);

wallet_p3a->ReportTransactionSent(mojom::CoinType::SOL, true);
histogram_tester_->ExpectUniqueSample(kSolTransactionSentHistogramName, 1, 1);

wallet_p3a->ReportTransactionSent(mojom::CoinType::FIL, true);
histogram_tester_->ExpectUniqueSample(kFilTransactionSentHistogramName, 1, 1);

task_environment_.FastForwardBy(base::Days(4));
histogram_tester_->ExpectUniqueSample(kEthTransactionSentHistogramName, 1, 5);
histogram_tester_->ExpectUniqueSample(kSolTransactionSentHistogramName, 1, 5);
histogram_tester_->ExpectUniqueSample(kFilTransactionSentHistogramName, 1, 5);

task_environment_.FastForwardBy(base::Days(3));
histogram_tester_->ExpectBucketCount(kEthTransactionSentHistogramName, 0, 1);
histogram_tester_->ExpectBucketCount(kSolTransactionSentHistogramName, 0, 1);
histogram_tester_->ExpectBucketCount(kFilTransactionSentHistogramName, 0, 1);
}

} // namespace brave_wallet
5 changes: 5 additions & 0 deletions browser/ui/webui/brave_wallet/wallet_page_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <string>
#include <utility>

#include "base/command_line.h"
#include "base/files/file_path.h"
#include "brave/browser/brave_wallet/asset_ratio_service_factory.h"
#include "brave/browser/brave_wallet/brave_wallet_service_factory.h"
Expand All @@ -25,6 +26,7 @@
#include "brave/components/brave_wallet/browser/keyring_service.h"
#include "brave/components/brave_wallet/browser/swap_service.h"
#include "brave/components/brave_wallet/browser/tx_service.h"
#include "brave/components/brave_wallet/common/brave_wallet.mojom-forward.h"
#include "brave/components/brave_wallet_page/resources/grit/brave_wallet_page_generated_map.h"
#include "brave/components/constants/webui_url_constants.h"
#include "brave/components/l10n/common/localization_util.h"
Expand Down Expand Up @@ -66,6 +68,9 @@ WalletPageUI::WalletPageUI(content::WebUI* web_ui)
source->AddString("braveWalletTrezorBridgeUrl", kUntrustedTrezorURL);
source->AddString("braveWalletNftBridgeUrl", kUntrustedNftURL);
source->AddString("braveWalletMarketUiBridgeUrl", kUntrustedMarketURL);
source->AddBoolean(brave_wallet::mojom::kP3ACountTestNetworksLoadTimeKey,
base::CommandLine::ForCurrentProcess()->HasSwitch(
brave_wallet::mojom::kP3ACountTestNetworksSwitch));
content::WebUIDataSource::Add(profile, source);
content::URLDataSource::Add(profile,
std::make_unique<SanitizedImageSource>(profile));
Expand Down
2 changes: 1 addition & 1 deletion browser/ui/webui/brave_wallet/wallet_page_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class WalletPageUI : public ui::MojoWebUIController,
mojo::PendingReceiver<brave_wallet::mojom::BraveWalletService>
brave_wallet_service,
mojo::PendingReceiver<brave_wallet::mojom::BraveWalletP3A>
brave_wallet_p3a_receiver) override;
brave_wallet_p3a) override;

std::unique_ptr<WalletPageHandler> page_handler_;
std::unique_ptr<WalletHandler> wallet_handler_;
Expand Down
17 changes: 13 additions & 4 deletions browser/ui/webui/brave_wallet/wallet_panel_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <utility>

#include "base/bind.h"
#include "base/command_line.h"
#include "brave/browser/brave_wallet/asset_ratio_service_factory.h"
#include "brave/browser/brave_wallet/brave_wallet_service_factory.h"
#include "brave/browser/brave_wallet/json_rpc_service_factory.h"
Expand All @@ -24,6 +25,7 @@
#include "brave/components/brave_wallet/browser/keyring_service.h"
#include "brave/components/brave_wallet/browser/swap_service.h"
#include "brave/components/brave_wallet/browser/tx_service.h"
#include "brave/components/brave_wallet/common/brave_wallet.mojom-forward.h"
#include "brave/components/brave_wallet_panel/resources/grit/brave_wallet_panel_generated_map.h"
#include "brave/components/constants/webui_url_constants.h"
#include "brave/components/l10n/common/localization_util.h"
Expand Down Expand Up @@ -66,7 +68,9 @@ WalletPanelUI::WalletPanelUI(content::WebUI* web_ui)
source->AddString("braveWalletTrezorBridgeUrl", kUntrustedTrezorURL);
source->AddString("braveWalletNftBridgeUrl", kUntrustedNftURL);
source->AddString("braveWalletMarketUiBridgeUrl", kUntrustedMarketURL);

source->AddBoolean(brave_wallet::mojom::kP3ACountTestNetworksLoadTimeKey,
base::CommandLine::ForCurrentProcess()->HasSwitch(
brave_wallet::mojom::kP3ACountTestNetworksSwitch));
if (ShouldDisableCSPForTesting()) {
source->DisableContentSecurityPolicy();
}
Expand Down Expand Up @@ -115,7 +119,9 @@ void WalletPanelUI::CreatePanelHandler(
mojo::PendingReceiver<brave_wallet::mojom::FilTxManagerProxy>
filecoin_tx_manager_proxy_receiver,
mojo::PendingReceiver<brave_wallet::mojom::BraveWalletService>
brave_wallet_service_receiver) {
brave_wallet_service_receiver,
mojo::PendingReceiver<brave_wallet::mojom::BraveWalletP3A>
brave_wallet_p3a_receiver) {
DCHECK(page);
auto* profile = Profile::FromWebUI(web_ui());
DCHECK(profile);
Expand All @@ -142,8 +148,11 @@ void WalletPanelUI::CreatePanelHandler(
profile, std::move(solana_tx_manager_proxy_receiver));
brave_wallet::TxServiceFactory::BindFilTxManagerProxyForContext(
profile, std::move(filecoin_tx_manager_proxy_receiver));
brave_wallet::BraveWalletServiceFactory::BindForContext(
profile, std::move(brave_wallet_service_receiver));
brave_wallet::BraveWalletService* wallet_service =
brave_wallet::BraveWalletServiceFactory::GetServiceForContext(profile);
wallet_service->Bind(std::move(brave_wallet_service_receiver));
wallet_service->GetBraveWalletP3A()->Bind(
std::move(brave_wallet_p3a_receiver));

auto* blockchain_registry = brave_wallet::BlockchainRegistry::GetInstance();
if (blockchain_registry) {
Expand Down
4 changes: 3 additions & 1 deletion browser/ui/webui/brave_wallet/wallet_panel_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ class WalletPanelUI : public ui::MojoBubbleWebUIController,
mojo::PendingReceiver<brave_wallet::mojom::FilTxManagerProxy>
fil_tx_manager_proxy,
mojo::PendingReceiver<brave_wallet::mojom::BraveWalletService>
brave_wallet_service) override;
brave_wallet_service,
mojo::PendingReceiver<brave_wallet::mojom::BraveWalletP3A>
brave_wallet_p3a) override;

std::unique_ptr<WalletPanelHandler> panel_handler_;
std::unique_ptr<WalletHandler> wallet_handler_;
Expand Down
56 changes: 56 additions & 0 deletions components/brave_wallet/browser/brave_wallet_p3a.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@

#include <utility>

#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/string_number_conversions.h"
#include "base/time/time.h"
#include "brave/components/brave_wallet/browser/brave_wallet_service.h"
#include "brave/components/brave_wallet/browser/keyring_service.h"
#include "brave/components/brave_wallet/browser/pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"

namespace brave_wallet {

Expand All @@ -22,6 +26,12 @@ const char kKeyringCreatedHistogramName[] = "Brave.Wallet.KeyringCreated";
const char kOnboardingConversionHistogramName[] =
"Brave.Wallet.OnboardingConversion.2";
const char kEthProviderHistogramName[] = "Brave.Wallet.EthProvider";
const char kEthTransactionSentHistogramName[] =
"Brave.Wallet.EthTransactionSent";
const char kSolTransactionSentHistogramName[] =
"Brave.Wallet.SolTransactionSent";
const char kFilTransactionSentHistogramName[] =
"Brave.Wallet.FilTransactionSent";

// Has the Wallet keyring been created?
// 0) No, 1) Yes
Expand Down Expand Up @@ -78,6 +88,12 @@ void BraveWalletP3A::Bind(
receivers_.Add(this, std::move(receiver));
}

void BraveWalletP3A::Update() {
ReportTransactionSent(mojom::CoinType::ETH, false);
ReportTransactionSent(mojom::CoinType::FIL, false);
ReportTransactionSent(mojom::CoinType::SOL, false);
}

void BraveWalletP3A::ReportEthereumProvider(
mojom::EthereumProviderType provider_type) {
UMA_HISTOGRAM_ENUMERATION(kEthProviderHistogramName, provider_type);
Expand All @@ -89,6 +105,46 @@ void BraveWalletP3A::ReportOnboardingAction(
onboarding_action);
}

void BraveWalletP3A::ReportTransactionSent(mojom::CoinType coin,
bool new_send) {
DictionaryPrefUpdate last_sent_time_update(
pref_service_, kBraveWalletLastTransactionSentTimeDict);
base::Value::Dict& last_sent_time_dict = last_sent_time_update->GetDict();

std::string coin_key = base::NumberToString(static_cast<int>(coin));

base::Time now = base::Time::Now();
base::Time last_sent_time = base::Time::FromDoubleT(
last_sent_time_dict.FindDouble(coin_key).value_or(0.0));

if (!new_send && last_sent_time.is_null()) {
// Don't report if a transaction was never sent.
return;
}
int answer = 0;
if (new_send || (now - last_sent_time) < base::Days(7)) {
answer = 1;
}
if (new_send) {
last_sent_time_dict.Set(coin_key, now.ToDoubleT());
}

const char* histogram_name;
switch (coin) {
case mojom::CoinType::ETH:
histogram_name = kEthTransactionSentHistogramName;
break;
case mojom::CoinType::SOL:
histogram_name = kSolTransactionSentHistogramName;
break;
case mojom::CoinType::FIL:
histogram_name = kFilTransactionSentHistogramName;
break;
}

base::UmaHistogramExactLinear(histogram_name, answer, 2);
}

void BraveWalletP3A::RecordInitialBraveWalletP3AState() {
keyring_service_->GetKeyringInfo(mojom::kDefaultKeyringId,
base::BindOnce(&RecordKeyringCreated));
Expand Down
6 changes: 6 additions & 0 deletions components/brave_wallet/browser/brave_wallet_p3a.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ extern const char kDefaultSolanaWalletHistogramName[];
extern const char kKeyringCreatedHistogramName[];
extern const char kOnboardingConversionHistogramName[];
extern const char kEthProviderHistogramName[];
extern const char kEthTransactionSentHistogramName[];
extern const char kSolTransactionSentHistogramName[];
extern const char kFilTransactionSentHistogramName[];

class BraveWalletService;
class KeyringService;
Expand All @@ -42,10 +45,13 @@ class BraveWalletP3A : public mojom::BraveWalletServiceObserver,
mojo::PendingRemote<mojom::BraveWalletP3A> MakeRemote();
void Bind(mojo::PendingReceiver<mojom::BraveWalletP3A> receiver);

void Update();

void ReportEthereumProvider(
mojom::EthereumProviderType provider_type) override;
void ReportOnboardingAction(
mojom::OnboardingAction onboarding_action) override;
void ReportTransactionSent(mojom::CoinType coin, bool new_send) override;

// KeyringServiceObserver
void KeyringCreated(const std::string& keyring_id) override;
Expand Down
3 changes: 3 additions & 0 deletions components/brave_wallet/browser/brave_wallet_p3a_private.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ void BraveWalletP3APrivate::ReportEthereumProvider(
void BraveWalletP3APrivate::ReportOnboardingAction(
mojom::OnboardingAction onboarding_action) {}

void BraveWalletP3APrivate::ReportTransactionSent(mojom::CoinType coin,
bool new_send) {}

} // namespace brave_wallet
1 change: 1 addition & 0 deletions components/brave_wallet/browser/brave_wallet_p3a_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class BraveWalletP3APrivate : public mojom::BraveWalletP3A {
mojom::EthereumProviderType provider_type) override;
void ReportOnboardingAction(
mojom::OnboardingAction onboarding_action) override;
void ReportTransactionSent(mojom::CoinType coin, bool new_send) override;
};

} // namespace brave_wallet
Expand Down
1 change: 1 addition & 0 deletions components/brave_wallet/browser/brave_wallet_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
p3a_utils::RegisterFeatureUsagePrefs(registry, kBraveWalletP3AFirstUnlockTime,
kBraveWalletP3ALastUnlockTime,
kBraveWalletP3AUsedSecondDay, nullptr);
registry->RegisterDictionaryPref(kBraveWalletLastTransactionSentTimeDict);
}

void RegisterProfilePrefsForMigration(
Expand Down
1 change: 1 addition & 0 deletions components/brave_wallet/browser/brave_wallet_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,7 @@ base::Value::Dict BraveWalletService::GetDefaultFilecoinAssets() {

void BraveWalletService::OnP3ATimerFired() {
RecordWalletUsage(false);
brave_wallet_p3a_.Update();
}

void BraveWalletService::OnWalletUnlockPreferenceChanged(
Expand Down
2 changes: 2 additions & 0 deletions components/brave_wallet/browser/pref_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ extern const char kBraveWalletP3AUsedSecondDay[] =
"brave.wallet.p3a_used_second_day";
const char kBraveWalletKeyringEncryptionKeysMigrated[] =
"brave.wallet.keyring_encryption_keys_migrated";
const char kBraveWalletLastTransactionSentTimeDict[] =
"brave.wallet.last_transaction_sent_time_dict";

// DEPRECATED
const char kBraveWalletSelectedAccount[] = "brave.wallet.selected_account";
Expand Down
Loading

0 comments on commit be63639

Please sign in to comment.