
If you have centrally defined metrics, Statsig can sync its data sources and metrics as part of your data version management workflow. Using Statsig's [Console API](/console-api/introduction), you can automatically sync changes to matching definitions in Statsig, and optionally make the metrics read-only in the Statsig console.

A demonstration [GitHub repository](https://github.com/statsig-io/semantic_layer) uses [a script](https://github.com/statsig-io/semantic_layer/blob/main/.github/scripts/statsig_sync.py) executed by [a GitHub Action](https://github.com/statsig-io/semantic_layer/blob/main/.github/workflows/statsig_sync.yml). This setup automatically synchronizes changes to .yml files in the /metrics or /metric\_sources directories in the repo. After you create or update these files, the script updates existing metrics or metric sources in Statsig, or creates new ones accordingly.

To use this example template, follow these steps:

1. Fork [this repository](https://github.com/statsig-io/semantic_layer) to get started.
2. In your forked repository, add your Statsig Console API Key to GitHub Secrets.
3. Update the metric definitions to match your data needs.
4. Validate the automation by modifying relevant files and observing the triggered GitHub Action.

## Detailed guide

### Forking the repository

1. **Fork this repository** to create a copy in your GitHub account. 

{% figure %}
<img src="/images/statsig-warehouse-native/configuration/semantic-layer-sync/79652104-6e13-467a-b4ef-dbbac83e15c9.png" alt="Untitled" width="1056" />
{% /figure %}

### Adding the Statsig Console API Key

2. Navigate to `Settings > Secrets and variables > Actions` in your repository settings. Create a new secret named `STATSIG_API_KEY` with your Statsig Console API key as its value. This key authenticates the synchronization process with the Statsig Console API. 

{% figure %}
<img src="/images/statsig-warehouse-native/configuration/semantic-layer-sync/0a627a98-3772-4c36-89d1-7587e3950e84.png" alt="Untitled" width="1168" />
{% /figure %}

### Customizing metric definitions

3. Metric definitions are in the `./metrics` directory, and metric source definitions are in the `./metric_sources/` directory. To customize:

   * Use the Statsig Console API to fetch an existing **metric\_source** or **metric** using GET requests for [metric sources](/console-api/introduction#post-/metrics/metric_source/-name-) and [metrics](/console-api/introduction#get-/metrics/-metric_id-).
   * Remove the provided example metrics and metric sources, and replace them with your definitions in `./metric_sources/*.yml` and `./metrics/*.yml`.

For readability, the examples use `metric.metricDefinition[]` instead of `metric.warehouseNative[]`. You can view this change [in the sync script](https://github.com/statsig-io/semantic_layer/blob/1611a68703caf18d7fa32088ff06d568d8b3b03a/.github/scripts/statsig_sync.py#L38). Adjust the mapping or revert to `metric.warehouseNative[]` in your definitions as needed.

### Verifying automation

4. To test, edit a metric or metric source description in your repository. This triggers the GitHub Action, visible under the `Actions` tab. The process then creates or updates your metrics and metric sources in Statsig based on the repository's semantic definitions.

{% figure %}
<img src="/images/statsig-warehouse-native/configuration/semantic-layer-sync/2dcf8961-3591-4021-88fe-984994177f35.png" alt="Untitled" width="1043" />
{% /figure %}

{% figure %}
<img src="/images/statsig-warehouse-native/configuration/semantic-layer-sync/04a084d6-ef6c-4b3f-aee9-41663ed47a60.png" alt="Untitled" width="1041" />
{% /figure %}

{% figure %}
<img src="/images/statsig-warehouse-native/configuration/semantic-layer-sync/4442dd9d-313f-4b16-a978-59513e4e8469.png" alt="Untitled" width="1031" />
{% /figure %}

<br />

This example is a basic template. Test and extend it to meet production standards. Share feedback or improvements with the Statsig support team, your sales contact, or in the [Slack community](https://statsig.com/slack).
