Logo

On-Chain Referral Systems for Web3 Attribution Tracking

December 4, 2023
On-chain referral tracking

Tracking referrals on-chain

In the evolving digital landscape, businesses and developers are constantly seeking the most effective tools for tracking and analyzing data. At Origin, we’ve been using Google Analytics as the go-to solution for monitoring web-based interactions, but blockchain technology introduces a compelling alternative: tracking transactions on-chain. This approach offers unique benefits, particularly around accuracy and transparency.

The basic premise behind tracking referrals on-chain is to append some extra bytes to the calldata of any transaction originating from a decentralized app (dapp) that you control. The extra bytes do not typically interfere with the contract call (though there are exceptions), but it does increase the gas cost of the transaction by a minuscule amount.

These extra bytes are stored on-chain and are visible on Etherscan or on any other indexer. In Origin’s case, we use Subsquid to tag transactions that have calldata ending with a known byte array, so that we know which transactions originated from our dapp.

This technique has been used by NFT marketplaces for some time and is how transaction volume can be attributed to aggregators like Blur or Gem. NFT infrastructure projects like Reservoir have built the functionality directly into their SDK, though the technique isn’t yet common within DeFi.

Implementing an on-chain referrer system is a relatively simple process, but there are a few key considerations. You’ll need to decide what kind of attribution to use (first touch vs. last touch), and how you’ll store the data. Here’s how I set up an on-chain referral system for Origin Protocol.

Here’s how it works from start to finish:

  • Create a 4 byte code by hashing a string, eg bytes4(keccak256("oeth.com")).
  • Store the hash somewhere, for example a code repo or spreadsheet, so that it can be referenced later.
  • Modify your dapp to accept referral codes via URL params, for example mydapp.com/?code=abc12345, and store that code in localStorage
  • When your dapp is submitting a transaction, append the referral code to the calldata
  • Once the transaction is mined, you will see that the referral code has been appended to the calldata and visible on Etherscan or via eth_getTransactionByHash RPC call.

Advantages of On-Chain Tracking

  • Immutable Records: The blockchain's immutable nature ensures that referral codes are recorded indefinitely and accessible by anyone.
  • Independent Verifiability. Third parties can independently verify the functionality of the referral feature without needing access to restricted data.
  • Accuracy. Unlike traditional tracking methods, this approach is not affected by ad-blockers and browser cookies.
  • Cross-domain. The method can be applied across different decentralized applications.
  • Open standard. Third party indexers are able to attribute transactions to aggregators or other third parties.

Potential Limitations

  • Increased Gas Costs. Adding extra bytes increases transaction costs slightly, about 16 gas per byte, for a total of 64 gas for 4 bytes.
  • Incompatible transactions. In some cases the extra calldata may cause third party wallets to malfunction. For example, common transactions such as ERC20 approvals can cause custom UX to be displayed in wallets like MetaMask. Extra calldata can cause errors in these cases, so it’s important to thoroughly test any implementation to ensure these types of issues don’t arise.

On-chain referral tracking offers some compelling advantages over traditional tracking mechanisms, such as its immutability, transparency, and resistance to common web tracking issues. While it does come with its own set of challenges, including increased gas costs and potential incompatibilities, the benefits likely outweigh these limitations and the technique is worth considering as an alternative.

Nick Poulden
Nick Poulden
Origin
Stay in touch
Be the first to hear about important product updates. Your email will be kept private.
Originally released by Origin Protocol
Privacy policyTerms of service