On-chain Web3 Marketing with Google and Chainlink

Allen Day
Google Cloud - Community
3 min readMar 1, 2023

Use Google Analytics Data in Web3 Smart Contracts with Google BigQuery and Chainlink Functions

Google Cloud and Chainlink have a rich and long-standing collaboration unlocking hybrid blockchain/cloud applications through the combination of Google Cloud’s Public Datasets Program and Chainlink oracles. These hybrid applications enable developers to bring real-world datasets onto blockchain networks to build next-generation solutions like parametric weather insurance. With the launch of Chainlink Functions, it is now easier than ever for developers to connect not only to Google Cloud’s Public Datasets but all of Google’s hundreds of APIs. This unlocks a new wave of Web3 use cases.

The tutorial below walks through how you can use Chainlink Functions to connect to Google Analytics data that has been uploaded to Google BigQuery and trigger on-chain smart contract logic based on this data.

Using Google BigQuery Data to Trigger On-Chain Logic

Google Analytics, is a popular web analytics software that was launched in 2005. As part of Google Marketing Platform, Google Analytics is designed to help web publishers deliver better eCommerce experiences to their customers. But Google Analytics is not Web3-enabled out of the box.

Google BigQuery is a cloud-native data warehouse that enables businesses to analyze and query large datasets in real-time.

Combining Google Analytics data with Google BigQuery enables advanced analysis and customization of data, including the creation of custom metrics, joining with other data sources, and performing machine learning tasks.

Chainlink Functions is a serverless developer platform for Web3 that enables smart contracts to connect to any data or API, as well as get decentralized and verifiable consensus on the result of any code that you want to run.

While Google BigQuery and Google Analytics were designed to be used with traditional web applications, Chainlink Functions now makes it possible to use BigQuery and Analytics data within decentralized applications.

In this tutorial, we’ll show how you can provide live updates about a website’s user statistics to an on-chain smart contract. We’ll be using Google Analytics to capture and automatically export data to Google BigQuery, and we’ll use Chainlink Functions and Chainlink Automation to handle the connection with Google BigQuery and the automatic updating of on-chain data.

For this particular example, a user is directed to a website where they vote for a dog or cat. The vote is registered with Google Analytics, which automatically stores data in Google BigQuery.

Chainlink Automation periodically calls a Chainlink Function, which in turn fetches vote totals from Google BigQuery and writes them to an on-chain smart contract. Once the voting period has ended, a function in the smart contract can be called to reveal the winner.

Here’s a diagram of the application’s architecture:

Application Architecture, Google {Analytics,BigQuery} + Chainlink Functions

Before a request to the Google BigQuery API can be made, the Chainlink Function must request an access token:

Once this token has been obtained, it can be used to fetch data from Google BigQuery for each selection:

The returned results are then combined and sent back on-chain to the consuming smart contract:

Once the voting period has completed, the `declareWinner` function will determine which of the two animals is the winner:

The complete source code for this tutorial can be found in the Google BigQuery Demo GitHub repository.

Conclusion

While the example above is limited to specific analytics data based upon two events, it showcases the new hybrid innovation space when you use Chainlink Functions to combine decentralized applications with cloud and analytics services such as Google BigQuery and Google Analytics.

Chainlink’s Web3 services platform expands the potential use cases for smart contracts by empowering developers to securely and easily access off-chain data and computation. As developers continue to connect more Google services to smart contracts using Chainlink, the possibilities for Web3 applications will only continue to expand.

To explore how Chainlink Functions transforms the Web3 developer experience, ​​visit Use Chainlink Functions, where you can see more examples or submit your own. To learn more about how Chainlink Functions works, visit https://chain.link/functions.

--

--