<@U07FJ89LFFB> Can you provide an example of a tra...
# ask-ai
w
@brash-pillow-71627 Can you provide an example of a tracking callback script for BigQuery (without using GA4)? I’m unsure how to match anonymous "user IDs" with purchases. Do I need to send the "user ID" (for non-logged-in users) in the tracking callback script?
o
A message from kapa.ai
Yes, you can use a tracking callback script to send data to BigQuery. The tracking callback function is used to log experiment exposures. When a user is assigned to a variant of an experiment, the tracking callback function is called with the experiment and result as arguments. Here's a general example of how a tracking callback function might look:
Copy code
javascript trackingCallback: function(experiment, result) {    // TODO: track experiment impression    dataLayer.push({       'event': 'experiment_viewed',       'user_id': gbuuid,       'experiment_id': experiment.key,       'variation_id': result.variationId,    }); }
In this example,
gbuuid
is the user ID,
experiment.key
is the experiment ID, and
result.variationId
is the variant ID. This function essentially adds a row to BigQuery that says "user123, saw variation 1, of this experiment, at this date/time". For non-logged-in users, you can generate a unique ID on the client side and use it as the "user ID". This ID should be persistent and used for other metric events as well. This way, you can match anonymous "user IDs" with purchases or other events. Remember, the ID you use for assignment/bucketing should also be available in BigQuery to join to the metric events. This means you should pass this ID to BigQuery so that the metrics will work. Sources: - [Forum Conversation > T > 16382394 Part 5](https://linen.growthbook.io/t/16382394/hello-i-am-facing-some-issue-with-growth-book-configuration-) - [Forum Conversation > T > 8145516 Part 2](https://linen.growthbook.io/t/8145516/hi-all-i-m-looking-into-using-growthbook-to-test-out-feature) - [Forum Conversation > T > 10358958](https://linen.growthbook.io/t/10358958/hey-i-am-having-a-landing-page-with-anonymous-users-i-am-pla)
w
@brash-pillow-71627 I don't understand how would that script send data to bigquery, I believe it is missing the id of the bigquery project among other info?
o
A message from kapa.ai
I apologize for the confusion. The example provided is a general structure of a tracking callback function. It doesn't directly send data to BigQuery. Instead, it prepares the data to be sent to BigQuery by another service, such as Google Analytics 4 (GA4), Segment, or RudderStack. The actual process of sending data to BigQuery would depend on the service you're using. For example, if you're using GA4, you would use the
gtag
function to send an event to GA4, which then sends the data to BigQuery. Here's an example of how you might do this with GA4:
Copy code
javascript trackingCallback: function(experiment, result) {    // track with GA4    gtag("event", "experiment_viewed", {       event_category: "experiment",       experiment_id: experiment.key,       variation_id: result.variationId,       gb_user_id: gbuuid,    }); }
In this example,
gtag
is a function provided by GA4 for sending events. The event data includes the experiment ID, variant ID, and user ID. GA4 then sends this data to BigQuery. If you're not using GA4 or another service that can send data to BigQuery, you would need to use a BigQuery client library to send the data directly from your application. This would involve creating a BigQuery client, creating a dataset and table in BigQuery, and inserting rows into the table. The details of this process would depend on the programming language you're using. Please refer to the [BigQuery documentation](https://cloud.google.com/bigquery/docs) for more information on how to send data to BigQuery directly from your application. Sources: - [Forum Conversation > T > 16382394 Part 5](https://linen.growthbook.io/t/16382394/hello-i-am-facing-some-issue-with-growth-book-configuration-) - [Forum Conversation > T > 8145516 Part 2](https://linen.growthbook.io/t/8145516/hi-all-i-m-looking-into-using-growthbook-to-test-out-feature) - [Forum Conversation > T > 10358958](https://linen.growthbook.io/t/10358958/hey-i-am-having-a-landing-page-with-anonymous-users-i-am-pla)