From 44a02ab22d6688e897bc246477100c60da1983db Mon Sep 17 00:00:00 2001 From: ryanml Date: Fri, 25 Sep 2020 12:31:11 -0700 Subject: [PATCH] Adding interactions API methods --- browser/extensions/api/moonpay_api.cc | 35 +++++++++++++++++++ browser/extensions/api/moonpay_api.h | 21 +++++++++++ common/extensions/api/moonpay.json | 33 +++++++++++++++++ .../actions/bitcoin_dot_com_actions.ts | 2 ++ .../default/bitcoinDotCom/index.tsx | 5 +++ .../components/default/bitcoinDotCom/style.ts | 2 +- .../constants/bitcoin_dot_com_types.ts | 3 +- .../containers/newTab/index.tsx | 5 +++ .../reducers/bitcoin_dot_com_reducer.ts | 4 +++ components/brave_new_tab_ui/reducers/index.ts | 2 ++ components/definitions/chromel.d.ts | 2 ++ .../moonpay/browser/moonpay_pref_utils.cc | 1 + .../moonpay/common/moonpay_pref_names.cc | 2 ++ .../moonpay/common/moonpay_pref_names.h | 1 + 14 files changed, 116 insertions(+), 2 deletions(-) diff --git a/browser/extensions/api/moonpay_api.cc b/browser/extensions/api/moonpay_api.cc index 70fe7f504cb..8a253207fbc 100644 --- a/browser/extensions/api/moonpay_api.cc +++ b/browser/extensions/api/moonpay_api.cc @@ -7,6 +7,7 @@ #include #include +#include #include "base/environment.h" #include "brave/browser/profiles/profile_util.h" @@ -42,9 +43,43 @@ MoonpayOnBuyBitcoinDotComCryptoFunction::Run() { } profile->GetPrefs()->SetBoolean(kMoonpayHasBoughtBitcoinDotComCrypto, true); + profile->GetPrefs()->SetBoolean(kMoonpayHasInteractedBitcoinDotCom, true); return RespondNow(NoArguments()); } +ExtensionFunction::ResponseAction +MoonpayOnInteractionBitcoinDotComFunction::Run() { + Profile* profile = Profile::FromBrowserContext(browser_context()); + + if (brave::IsTorProfile(profile)) { + return RespondNow(Error("Not available in Tor profile")); + } + + profile->GetPrefs()->SetBoolean(kMoonpayHasInteractedBitcoinDotCom, true); + + return RespondNow(NoArguments()); +} + +ExtensionFunction::ResponseAction +MoonpayGetBitcoinDotComInteractionsFunction::Run() { + Profile* profile = Profile::FromBrowserContext(browser_context()); + + if (brave::IsTorProfile(profile)) { + return RespondNow(Error("Not available in Tor profile")); + } + + bool has_bought = profile->GetPrefs()->GetBoolean( + kMoonpayHasBoughtBitcoinDotComCrypto); + bool has_interacted = profile->GetPrefs()->GetBoolean( + kMoonpayHasInteractedBitcoinDotCom); + + auto interactions = std::make_unique(); + interactions->SetBoolean("boughtCrypto", has_bought); + interactions->SetBoolean("interacted", has_interacted); + + return RespondNow(OneArgument(std::move(interactions))); +} + } // namespace api } // namespace extensions diff --git a/browser/extensions/api/moonpay_api.h b/browser/extensions/api/moonpay_api.h index e610332791a..58fde8d05bb 100644 --- a/browser/extensions/api/moonpay_api.h +++ b/browser/extensions/api/moonpay_api.h @@ -36,6 +36,27 @@ class MoonpayOnBuyBitcoinDotComCryptoFunction : ResponseAction Run() override; }; +class MoonpayOnInteractionBitcoinDotComFunction : + public ExtensionFunction { + public: + DECLARE_EXTENSION_FUNCTION("moonpay.onInteractionBitcoinDotCom", UNKNOWN) + + protected: + ~MoonpayOnInteractionBitcoinDotComFunction() override {} + ResponseAction Run() override; +}; + +class MoonpayGetBitcoinDotComInteractionsFunction : + public ExtensionFunction { + public: + DECLARE_EXTENSION_FUNCTION("moonpay.getBitcoinDotComInteractions", UNKNOWN) + + protected: + ~MoonpayGetBitcoinDotComInteractionsFunction() override {} + ResponseAction Run() override; +}; + + } // namespace api } // namespace extensions diff --git a/common/extensions/api/moonpay.json b/common/extensions/api/moonpay.json index 75e8bff2295..02912b1451d 100644 --- a/common/extensions/api/moonpay.json +++ b/common/extensions/api/moonpay.json @@ -34,6 +34,39 @@ "type": "function", "description": "Marks when a user has used the bitcoin.com widget to buy crypto", "parameters": [] + }, + { + "name": "onInteractionBitcoinDotCom", + "type": "function", + "description": "Marks user interaction with the bitcoin.com widget", + "parameters": [] + }, + { + "name": "getBitcoinDotComInteractions", + "type": "function", + "description": "Fetches user interactions with bitcoin.com", + "parameters": [ + { + "type": "function", + "name": "callback", + "parameters": [ + { + "name": "interactions", + "type": "object", + "properties": { + "boughtCrypto": { + "type": "boolean", + "description": "Has clicked the buy button" + }, + "interacted": { + "type": "boolean", + "description": "Has interacted" + } + } + } + ] + } + ] } ], "types": [ diff --git a/components/brave_new_tab_ui/actions/bitcoin_dot_com_actions.ts b/components/brave_new_tab_ui/actions/bitcoin_dot_com_actions.ts index a4f3dc4ca27..91339768e28 100644 --- a/components/brave_new_tab_ui/actions/bitcoin_dot_com_actions.ts +++ b/components/brave_new_tab_ui/actions/bitcoin_dot_com_actions.ts @@ -7,3 +7,5 @@ import { action } from 'typesafe-actions' import { types } from '../constants/bitcoin_dot_com_types' export const buyBitcoinDotComCrypto = () => action(types.BUY_BITCOIN_DOT_COM_CRYPTO) + +export const interactionBitcoinDotCom = () => action(types.INTERACTION_BITCOIN_DOT_COM) diff --git a/components/brave_new_tab_ui/components/default/bitcoinDotCom/index.tsx b/components/brave_new_tab_ui/components/default/bitcoinDotCom/index.tsx index 9e5079d5885..360130d2cc1 100644 --- a/components/brave_new_tab_ui/components/default/bitcoinDotCom/index.tsx +++ b/components/brave_new_tab_ui/components/default/bitcoinDotCom/index.tsx @@ -25,6 +25,7 @@ interface Props { stackPosition: number onShowContent: () => void onBuyCrypto: () => void + onInteraction: () => void } class BitcoinDotCom extends React.PureComponent { @@ -72,6 +73,7 @@ class BitcoinDotCom extends React.PureComponent { this.setState({ assetsShowing: !this.state.assetsShowing }) + this.props.onInteraction() } setSelectedAsset = (asset: string) => { @@ -85,12 +87,14 @@ class BitcoinDotCom extends React.PureComponent { this.setState({ currentAmount: target.value }) + this.props.onInteraction() } toggleFiatCurrenciesShowing = () => { this.setState({ fiatCurrenciesShowing: !this.state.fiatCurrenciesShowing }) + this.props.onInteraction() } setSelectedFiat = (fiat: string) => { @@ -126,6 +130,7 @@ class BitcoinDotCom extends React.PureComponent { openInfoURL = () => { window.open('https://bitcoincom.moonpay.io/', '_blank', 'noopener') + this.props.onInteraction() } renderCurrencyInput () { diff --git a/components/brave_new_tab_ui/components/default/bitcoinDotCom/style.ts b/components/brave_new_tab_ui/components/default/bitcoinDotCom/style.ts index 16115746218..0cebd60ded8 100644 --- a/components/brave_new_tab_ui/components/default/bitcoinDotCom/style.ts +++ b/components/brave_new_tab_ui/components/default/bitcoinDotCom/style.ts @@ -43,7 +43,7 @@ export const BitcoinDotComIcon = styled<{}, 'div'>('div')` ` export const TitleText = styled<{}, 'div'>('div')` - margin-top: 4px; + margin-top: -2px; ` export const InputHeader = styled<{}, 'div'>('div')` diff --git a/components/brave_new_tab_ui/constants/bitcoin_dot_com_types.ts b/components/brave_new_tab_ui/constants/bitcoin_dot_com_types.ts index 0348342cc3f..fd5c8f045eb 100644 --- a/components/brave_new_tab_ui/constants/bitcoin_dot_com_types.ts +++ b/components/brave_new_tab_ui/constants/bitcoin_dot_com_types.ts @@ -4,5 +4,6 @@ // you can obtain one at http://mozilla.org/MPL/2.0/. export const enum types { - BUY_BITCOIN_DOT_COM_CRYPTO = '@@bitcoin_dot_com/BUY_BITCOIN_DOT_COM_CRYPTO' + BUY_BITCOIN_DOT_COM_CRYPTO = '@@bitcoin_dot_com/BUY_BITCOIN_DOT_COM_CRYPTO', + INTERACTION_BITCOIN_DOT_COM = '@@bitcoin_dot_com/INTERACTION_BITCOIN_DOT_COM' } diff --git a/components/brave_new_tab_ui/containers/newTab/index.tsx b/components/brave_new_tab_ui/containers/newTab/index.tsx index 7baf59b8fa5..258b701ac83 100644 --- a/components/brave_new_tab_ui/containers/newTab/index.tsx +++ b/components/brave_new_tab_ui/containers/newTab/index.tsx @@ -410,6 +410,10 @@ class NewTabPage extends React.Component { this.props.actions.buyBitcoinDotComCrypto() } + onInteractionBitcoinDotCom = () => { + this.props.actions.interactionBitcoinDotCom() + } + onBinanceUserTLD = (userTLD: NewTab.BinanceTLD) => { this.props.actions.onBinanceUserTLD(userTLD) } @@ -941,6 +945,7 @@ class NewTabPage extends React.Component { showContent={showContent} onShowContent={this.setForegroundStackWidget.bind(this, 'bitcoinDotCom')} onBuyCrypto={this.onBuyBitcoinDotComCrypto} + onInteraction={this.onInteractionBitcoinDotCom} lightWidget={showContent} /> ) diff --git a/components/brave_new_tab_ui/reducers/bitcoin_dot_com_reducer.ts b/components/brave_new_tab_ui/reducers/bitcoin_dot_com_reducer.ts index 33b4ca0643e..c21361b483a 100644 --- a/components/brave_new_tab_ui/reducers/bitcoin_dot_com_reducer.ts +++ b/components/brave_new_tab_ui/reducers/bitcoin_dot_com_reducer.ts @@ -11,6 +11,10 @@ const bitcoinDotComReducer: Reducer = (state: NewTab.S chrome.moonpay.onBuyBitcoinDotComCrypto() break + case types.INTERACTION_BITCOIN_DOT_COM: + chrome.moonpay.onInteractionBitcoinDotCom() + break + default: break } diff --git a/components/brave_new_tab_ui/reducers/index.ts b/components/brave_new_tab_ui/reducers/index.ts index b519c478100..cc86e74dd70 100644 --- a/components/brave_new_tab_ui/reducers/index.ts +++ b/components/brave_new_tab_ui/reducers/index.ts @@ -12,6 +12,7 @@ import gridSitesReducer from './grid_sites_reducer' import binanceReducer from './binance_reducer' import rewardsReducer from './rewards_reducer' import geminiReducer from './gemini_reducer' +import bitcoinDotComReducer from './bitcoin_dot_com_reducer' export const newTabReducers = (state: NewTab.State | undefined, action: any) => { if (state === undefined) { @@ -23,6 +24,7 @@ export const newTabReducers = (state: NewTab.State | undefined, action: any) => state = binanceReducer(state, action) state = rewardsReducer(state, action) state = geminiReducer(state, action) + state = bitcoinDotComReducer(state, action) if (state !== startingState) { storage.debouncedSave(state) diff --git a/components/definitions/chromel.d.ts b/components/definitions/chromel.d.ts index 1179bf97c47..38f554dc0fc 100644 --- a/components/definitions/chromel.d.ts +++ b/components/definitions/chromel.d.ts @@ -198,6 +198,8 @@ declare namespace chrome.braveTogether { declare namespace chrome.moonpay { const isBitcoinDotComSupported: (callback: (supported: boolean) => void) => {} const onBuyBitcoinDotComCrypto: () => void + const onInteractionBitcoinDotCom: () => void + const getBitcoinDotComInteractions: (callback: (interactions: Record) => void) => {} } declare namespace chrome.rewardsNotifications { diff --git a/components/moonpay/browser/moonpay_pref_utils.cc b/components/moonpay/browser/moonpay_pref_utils.cc index 9c80eb5d46b..d7588585ee6 100644 --- a/components/moonpay/browser/moonpay_pref_utils.cc +++ b/components/moonpay/browser/moonpay_pref_utils.cc @@ -18,6 +18,7 @@ MoonpayPrefUtils::~MoonpayPrefUtils() {} void MoonpayPrefUtils::RegisterPrefs(PrefRegistrySimple* registry) { registry->RegisterBooleanPref(kMoonpayNewTabPageShowBitcoinDotCom, true); registry->RegisterBooleanPref(kMoonpayHasBoughtBitcoinDotComCrypto, false); + registry->RegisterBooleanPref(kMoonpayHasInteractedBitcoinDotCom, false); } } // namespace moonpay diff --git a/components/moonpay/common/moonpay_pref_names.cc b/components/moonpay/common/moonpay_pref_names.cc index 23dc85bd9aa..22a62598d16 100644 --- a/components/moonpay/common/moonpay_pref_names.cc +++ b/components/moonpay/common/moonpay_pref_names.cc @@ -7,5 +7,7 @@ const char kMoonpayHasBoughtBitcoinDotComCrypto[] = "moonpay.has_bought_bitcoin_dot_com_crypto"; +const char kMoonpayHasInteractedBitcoinDotCom[] = + "moonpay.has_interacted_bitcoin_dot_com"; const char kMoonpayNewTabPageShowBitcoinDotCom[] = "moonpay.new_tab_page.show_bitcoin_dot_com"; diff --git a/components/moonpay/common/moonpay_pref_names.h b/components/moonpay/common/moonpay_pref_names.h index 7e9ca4e8a22..978f0699cca 100644 --- a/components/moonpay/common/moonpay_pref_names.h +++ b/components/moonpay/common/moonpay_pref_names.h @@ -7,6 +7,7 @@ #define BRAVE_COMPONENTS_MOONPAY_COMMON_MOONPAY_PREF_NAMES_H_ extern const char kMoonpayHasBoughtBitcoinDotComCrypto[]; +extern const char kMoonpayHasInteractedBitcoinDotCom[]; extern const char kMoonpayNewTabPageShowBitcoinDotCom[]; #endif // BRAVE_COMPONENTS_MOONPAY_COMMON_MOONPAY_PREF_NAMES_H_