From 9bd391a35fa653c1285b428795ae718bfa3546eb Mon Sep 17 00:00:00 2001 From: Olusegun Akintayo Date: Sun, 10 Apr 2022 11:42:00 +0400 Subject: [PATCH] User trait - number of nft collections (#14377) * Add new user trait for 'Number of NFT collections'. Signed-off-by: Akintayo A. Olusegun FIx JS DOC Signed-off-by: Akintayo A. Olusegun Arrange TRAITS in alphabetical order Signed-off-by: Akintayo A. Olusegun Unit Tests for allCollectibles traits tracking. Signed-off-by: Akintayo A. Olusegun change cid to chainId Signed-off-by: Akintayo A. Olusegun * invert condition Signed-off-by: Akintayo A. Olusegun * jsdoc - alphabetical order Signed-off-by: Akintayo A. Olusegun * change {string} to the literal {number_of_nft_collections} Signed-off-by: Akintayo A. Olusegun * Optimize _getNumberOfNFTs Signed-off-by: Akintayo A. Olusegun --- app/scripts/controllers/metametrics.js | 22 +++++++++++++++ app/scripts/controllers/metametrics.test.js | 31 +++++++++++++++++++-- shared/constants/metametrics.js | 7 +++-- 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/app/scripts/controllers/metametrics.js b/app/scripts/controllers/metametrics.js index a3fd4eead..4cc3ed69e 100644 --- a/app/scripts/controllers/metametrics.js +++ b/app/scripts/controllers/metametrics.js @@ -540,6 +540,9 @@ export default class MetaMetricsController { (rpc) => rpc.chainId, ), [TRAITS.THREE_BOX_ENABLED]: metamaskState.threeBoxSyncingAllowed, + [TRAITS.NUMBER_OF_NFT_COLLECTIONS]: this._getNumberOfNFtCollection( + metamaskState, + ), }; if (!this.previousTraits) { @@ -581,6 +584,25 @@ export default class MetaMetricsController { }, {}); } + /** + * + * @param {object} metamaskState + * @returns number of unique collectible addresses + */ + _getNumberOfNFtCollection(metamaskState) { + const { allCollectibles } = metamaskState; + if (!allCollectibles) { + return 0; + } + + const allAddresses = Object.values(allCollectibles) + .flatMap((chainCollectibles) => Object.values(chainCollectibles)) + .flat() + .map((collectible) => collectible.address); + const unique = [...new Set(allAddresses)]; + return unique.length; + } + /** * Calls segment.identify with given user traits * diff --git a/app/scripts/controllers/metametrics.test.js b/app/scripts/controllers/metametrics.test.js index 9d5819f2d..81ea8e0ed 100644 --- a/app/scripts/controllers/metametrics.test.js +++ b/app/scripts/controllers/metametrics.test.js @@ -619,14 +619,41 @@ describe('MetaMetricsController', function () { ], ledgerTransportType: 'web-hid', identities: [{}, {}], + allCollectibles: { + '0xac706cE8A9BF27Afecf080fB298d0ee13cfb978A': { + 56: [ + { + address: '0xd2cea331e5f5d8ee9fb1055c297795937645de91', + tokenId: '100', + }, + { + address: '0xd2cea331e5f5d8ee9fb1055c297795937645de91', + tokenId: '101', + }, + { + address: '0x7488d2ce5deb26db021285b50b661d655eb3d3d9', + tokenId: '99', + }, + ], + }, + '0xe04AB39684A24D8D4124b114F3bd6FBEB779cacA': { + 69: [ + { + address: '0x63d646bc7380562376d5de205123a57b1718184d', + tokenId: '14', + }, + ], + }, + }, threeBoxSyncingAllowed: false, }); assert.deepEqual(traits, { - [TRAITS.THREE_BOX_ENABLED]: false, [TRAITS.LEDGER_CONNECTION_TYPE]: 'web-hid', - [TRAITS.NUMBER_OF_ACCOUNTS]: 2, [TRAITS.NETWORKS_ADDED]: [MAINNET_CHAIN_ID, ROPSTEN_CHAIN_ID], + [TRAITS.NUMBER_OF_ACCOUNTS]: 2, + [TRAITS.NUMBER_OF_NFT_COLLECTIONS]: 3, + [TRAITS.THREE_BOX_ENABLED]: false, }); }); diff --git a/shared/constants/metametrics.js b/shared/constants/metametrics.js index ffcfc4012..8291f4860 100644 --- a/shared/constants/metametrics.js +++ b/shared/constants/metametrics.js @@ -162,6 +162,8 @@ * identify the networks_added trait * @property {string} [NUMBER_OF_ACCOUNTS] - when identities change, we * identify the new number_of_accounts trait + * @property {number_of_nft_collections} [NUMBER_OF_NFT_COLLECTIONS] - user trait for number of + * unique NFT addresses * @property {string} [THREE_BOX_ENABLED] - when 3box feature is toggled we * identify the 3box_enabled trait */ @@ -173,9 +175,10 @@ export const TRAITS = { LEDGER_CONNECTION_TYPE: 'ledger_connection_type', - THREE_BOX_ENABLED: 'three_box_enabled', - NUMBER_OF_ACCOUNTS: 'number_of_accounts', NETWORKS_ADDED: 'networks_added', + NUMBER_OF_ACCOUNTS: 'number_of_accounts', + NUMBER_OF_NFT_COLLECTIONS: 'number_of_nft_collections', + THREE_BOX_ENABLED: 'three_box_enabled', }; // Mixpanel converts the zero address value to a truly anonymous event, which