Analytics
Platform

How we brought GitOps to GrowthBook with gbsync

How we brought GitOps to GrowthBook with gbsync

Joyn chose GrowthBook because its UI made it easier for other teams to set up experiments without our help. As our SQL grew more complex, we wanted to review changes before they reached GrowthBook. We built gbsync to manage those definitions in Git, with a workflow our Terraform users would recognise.

Moving from an in-house platform to GrowthBook

Before GrowthBook, we built and maintained our own experimentation platform. We had complete control over the backend and data, but the UI was always a pain point. When another team wanted to launch an experiment, we often had to walk them through it.

GrowthBook's intuitive UI drew us in, and its other features made it easy to get started. We connected our Snowflake datasource, created a few fact tables and metrics in the UI, and imported a couple of older experiments. They worked flawlessly.

Why we wanted metrics in Git

From the start, we needed a maintainable way to migrate our existing metrics into GrowthBook.

Some of our Snowflake queries are hundreds of lines long and run against large event datasets. When experiment results refresh, those queries run again. An inefficient query can take a long time and cost money, so we wanted a second pair of eyes on the SQL before a change went live.

The UI made it easy to create fact tables and metrics. We also wanted to see each change in a merge request, where someone could review it. We kept using the UI for experiments and moved the metric definitions into files.

Terraform, but for GrowthBook metrics

We built gbsync to use GrowthBook's documented API in a developer-friendly way. Our team was already using Terraform, so we wanted the workflow to feel familiar:

  1. Describe the desired state in files.
  2. Run plan to see what would change.
  3. Run apply to make it so.

gbsync is written in Python. It reads gbsync.yaml and uses the GrowthBook REST API to manage fact tables, filters, fact metrics, projects and environments.

Here's a fact table with its SQL in a separate file and a metric that uses it. The metric's numerator also refers to a conversions fact table, which isn't shown here:

factTables:
  - id: fact_table_events
    data:
      name: events
      datasource: ds_YOUR_DATASOURCE_ID
      userIdTypes: [user_id]
      sql: !include fact_tables/events.sql
factMetrics:
  - id: metric_conversion_rate
    data:
      name: Conversion Rate
      metricType: ratio
      numerator:
        factTableId: fact_table_conversions
        column: converted
      denominator:
        factTableId: fact_table_events
        column: total_events

Running gbsync plan compares the configuration with GrowthBook and lists the items it would create, update or delete. For example:

  To create: 0 table(s), 0 filter(s), 1 metric(s), 0 project(s), 0 environment(s)
    - Metric: Conversion Rate
  To update: 1 table(s), 0 filter(s), 0 metric(s), 0 project(s), 0 environment(s)
    - Table: events

(Abbreviated. plan also prints a table of managed and unmanaged resources.)

The plan lists the affected items, while the git diff shows the line-by-line changes a teammate can review. The plan doesn't run the SQL or estimate its cost, so we still review and test the query itself.

How we review and ship a metric change

We have staging and production GrowthBook environments. A metric change starts with a merge request, where a teammate can review the SQL and the Git diff. We use gbsync to preview and apply the definitions, and we can check them in staging before they reach production.

For example, this change updates upsell_rate from a ratio to a proportion and removes the explicit denominator:

       name: upsell_rate
-      metricType: ratio
+      metricType: proportion
+      description: Proportion of users in the variant who converted from free/trial to paid
       numerator:
         factTableId: upsell_events
         column: $$count
         filters:
           - upsells
-      denominator:
-        factTableId: upsell_events
-        column: $$count

Make every metric more sensitive

Reviewed metric definitions are a good start. Learn five ways to cut variance so the same metrics detect smaller effects.

Explore Variance Reduction

Why we built it this way

We chose plan and apply because the team already knew Terraform. plan shows what will change. apply asks for confirmation unless you pass --auto-approve, which we only use in CI.

We keep SQL in separate files. Our seven fact tables contain about 900 lines of Snowflake SQL, and the largest query is over 340 lines. With fact_tables/*.sql, reviewers get syntax highlighting, line comments and a diff. They can check the date window, how much data the query scans and whether it does unnecessary work. The YAML's !include tag loads the file at sync time, leaving GrowthBook variables such as {{ startDate }} untouched.

Resources created through the API are marked as API-managed. GrowthBook shows them as official and doesn't allow UI edits. That prevents someone from making a UI change that a later apply would overwrite.

If a metric disappears from the YAML, the next apply can delete it from GrowthBook. This applies to API-managed resources, including those managed through the API outside gbsync, so it's important to review deletions in the plan. UI-created items are ignored, and environments aren't deleted. Deletions follow dependency order: metrics, filters, then tables.

gbsync sends creates and updates for tables, filters and metrics to GrowthBook's bulk import endpoint in a single request.

The GitOps template

We also wanted the datasource configuration in Git. It defines the warehouse connection, exposure and identity-join queries, and experiment dimensions. For that, we built growthbook-gitops-template.

The template keeps two sets of files in Git:

  • gbsync.yaml and fact_tables/ contain analytics objects, synced with gbsync plan and apply.
  • config.yml.j2 and datasource_queries/ define the datasource. Experiment dimensions such as device and user_type are listed in the configuration and returned by the exposure query. A script renders the template with credentials from a secrets store, and the resulting configuration is baked into the GrowthBook Docker image.

The template uses fake example data, AWS Secrets Manager for credentials and Snowflake as the warehouse. You can replace the secrets store or warehouse without changing gbsync itself.

The SQL in the two directories is handled differently. Files in fact_tables/ go to GrowthBook untouched, so {{ startDate }} works as written. Files in datasource_queries/ are rendered by Jinja2 first. There, {{ startDate }} resolves to an empty string and breaks the query. Escaping it makes Jinja write the literal text back out:

WHERE assigned_date >= '{{ '{{ startDate }}' }}'

Running it in production at Joyn

In production, we manage 85 fact metrics, 7 fact tables and 2 environments this way, along with a set of fact table filters. The fact tables contain about 900 lines of SQL.

What's next

gbsync is moving into GrowthBook. You can try it with the template against a test instance and share what's missing.

Links:

Run your metrics on GrowthBook

Connect your warehouse, define fact tables and metrics, and manage them in the UI or as code through the API.

Get Started

Get The Uplift in your inbox

Experimentation tips and a new game, once a month.

Nil Patra

Nilabhra Patra

ML engineer

Table of Contents

Subscribe to the Uplift newsletter

Related articles

See All Articles
Compare
Platform
Why migrate from LaunchDarkly to GrowthBook
Product Updates
Platform
Introducing EU data residency for GrowthBook Cloud
Sample Ratio Mismatch (SRM): How to Identify the Root Cause and Decide Whether to Restart Your Experiment
Experiments
Analytics
Sample ratio mismatch (SRM): how to identify the root cause and decide whether to restart your experiment

Ready to ship faster?

No credit card required. Start with feature flags, experimentation, and product analytics — free.

Simplified white illustration of a right angle ruler or carpenter's square tool.White checkmark symbol with a scattered pixelated effect around its edges on a transparent background.