Source Control

Info

This workflow requires Ampli CLI 1.9.0+


Larger organizations with multiple teams usually need to make changes to their tracking plan and analytics implementations in parallel.

This page covers how you can use branches in Ampli with source control systems such as Git.

Workflow summary

When you need to update the tracking plan:

  1. Create a branch in Amplitude Data. Make tracking plan updates.
  2. Create a branch in Git.
  3. Pull the latest generated code for your Data branch into your Git branch with ampli pull.
  4. Implement the changes using the Ampli Wrapper in your code on your Git branch
  5. Check implementation status with ampli status.
  6. Merge the Data branch.
  7. Check merge status with ampli status --is-merged.
  8. Merge the Git branch.

Branches in Ampli

Ampli CLI

The Ampli CLI is able to pull down generated Ampli Wrapper code for Sources and Branches you have created in Data.

The first time you run ampli pull you're prompted to select a Source and Branch from your tracking plan.
This creates an ampli.json file to store your configuration for future development.

1ampli pull
2Ampli project is not initialized. No existing 'ampli.json' configuration found.
3? Create a new Ampli project here? Yes
4Organization: Amplitude Website (Portfolio)
5? Select a workspace: Data
6? Select a source: browser
7Source: browser
8Runtime: Browser/TypeScript
9? Select a branch: feature-branch-name
10Pulling version 74 (latest)

If later you would like to change branches you have a couple of options.

ampli checkout [my-branch-name]

This pulls down the specified provided branch from Data to your Ampli Wrapper. If you don't specify a branch name, you're prompted
to select one from a list of active branches in your tracking plan.

1ampli checkout
2? Select a branch
3 feature-branch-name (current)
4 add-products-to-cart
5remove-old-forms
6 new-registration-metrics
7(Move up and down to reveal more choices)

ampli pull -b my-branch-name

The --branch flag on ampli pull sets the tracking plan to use for the generated source code.

1ampli checkout feature-branch-name
2Source: browser
3Runtime: Browser/TypeScript
4Branch: feature-branch-name
5Pulling version 74 (latest)
6All events are up to date.

ampli.json

The ampli.json file contains configuration information for the Ampli CLI. This includes information about the current
branch and version. Running ampli pull and ampli checkout updates the ampli.json based on the selected tracking plan.

1{
2 "OrgId": "12345",
3 "WorkspaceId": "my-workspace-id",
4 "SourceId": "my-source-id",
5 "Runtime": "node.js:typescript-ampli"
6 "Path": "./path/to/ampli",
7 "Branch": "main",
8 "Version": "1.0.0",
9}

Note

This file is managed by the Ampli CLI and isn't meant to be modified by hand. That said it is mostly human readable to make any potential merge conflicts easier to understand.

Coordination and enforcement

The Ampli CLI provides checks to help teams coordinate development across branches.

ampli status

In addition to verifying implementation status of the tracking plan in your source code, the status command has some other checks specifically for branching functionality.

Example branch structure

1b1 1 -> 2 -> 3
2 / \
3main 1 -> 2 -> 3
4 \ \ /
5b2 \ 1 -> 2 -> 3 -> 4 -> 5
6 \
7b3 1 -> 2 -> 3 -> 4

Check if current Ampli branch is merged

ampli status --is-merged

1ampli status --is-merged
2Verifying event tracking implementation in source code
3Event A (1 location)
4Event B (1 location)
5All events tracked 2 found, 2 total
6WARNING The current branch branch-mapping version 74 has already been merged into main. Verified implementation against main version 75.
7We recommend running ampli pull to update to the latest tracking plan.
8Branch branch-mapping version 74 is merged.

The --is-merged flag checks the current Branch and Version in your ampli.json. If the exact Version of the Branch was merged into Ampli main it succeeds, otherwise it exits with error code 1. It always return success for the Ampli main branch.

For the branch structure example,, is-merged would return success for b1@3, b2@5, main@3, main@2, main@1.

Check if current Ampli branch is on the latest version

ampli status --is-latest

1ampli status --is-latest
2Verifying event tracking implementation in source code
3Event A (1 location)
4Event B (1 location)
5All events tracked 2 found, 2 total
6You are on the latest version of feature-branch-name

The --is-latest flag checks the Branch and Version in the ampli.json. If the Version is the last one published for the Branch it succeeds, otherwise it exits with error code 1.

For the branch structure example, is-latest would return success for b2@5, b3@4, main@3.

Note

Notice that b1@3 doesn't pass the is-latest check. This is because it maps to an outdated version of main@2.

For merged branches is-latest will be successful only if it contains the exact version that corresponds to the latest version of main.

The --is-latest-if-not-on-default-branch works the same as is-latest but also returns success for any version of the Ampli main branch. This is useful for CI integrations where you don't want to enforce using the latest version on the main branch.

For the branch structure example, is-latest-if-not-on-default-branch would return success for b2@5 , b3@4, main@3, main@2, main@1.

Combine checks

When you combine multiple checks ampli status returns success only if all checks pass. A failure in any check results in an error exit code.

When multiple checks run the status of each is output on a separate line with individual Errors and Warnings.

Common errors and solutions

1ampli status --is-merged --is-latest
2Verifying event tracking implementation in source code
3Event A
4Event B
5ERROR Event tracking incomplete 2 missed, 2 total
6ERROR Branch feature-branch-name version 73 is not merged.
7ERROR Branch feature-branch-name latest version is 74. You are on 73. Run ampli pull to update.

Here are some common errors and solutions for the ampli status command.

Event tracking implementation error

  • This means you haven't correctly instrumented all events in your tracking plan.
  • Implement the missing events in your project using the Ampli Wrapper.

is-merged error

This means you are currently on an un-merged Ampli branch. To fix this error:

  1. Log into Data and check to make sure your tracking plan has been reviewed and approved.
  2. You may need to "Refresh" your Data branch before being able to merge. If so make sure to run ampli pull again to
    get the latest updates.
    Merge your Data branch.

is-latest error

  • This means you aren't on the latest version of your Data branch.
  • Run ampli pull to update your local project.

Enforce branch coordination

ampli status --is-merged --is-latest

1ampli status --is-merged --is-latest
2Verifying event tracking implementation in source code
3Event A (1 location)
4Event B (1 location)
5All events tracked 2 found, 2 total
6WARNING The current branch feature-branch-name version 74 has already been merged into main. Verified implementation against main version 75.
7We recommend running ampli pull to update to the latest tracking plan.
8Branch feature-branch-name version 74 is merged.
9You are on the latest version of feature-branch-name

You can make sure that your Ampli and Git branches stay in sync with the ampli status command.

Before merging your Git branch with implementation changes you should verify that both is-merged and is-latest checks pass successfully. You can do this with a single command ampli status --is-merged --is-latest.

Info

Amplitude recommends merging your Git branch immediately after you merge your Data branch. This ensures other teams will have your implementation of the new events.

CI workflows

Add branch status checks in CI to make sure that code follows best practices.

Amplitude recommends adding new status checks to all your PR workflows. In addition to ampli status -u* you should add also add a call to ampli status --is-merged.

You can combine these into a single command, but for CI it can be useful to keep them separate. This helps you to see if the implementation checks succeed independently from the branch checks.

  • In PR workflows Amplitude recommends using --skip-update-on-default-branch when calling --update or -u.

Use is-latest in CI

Although it's not required, use is-latest in CI workflows for stricter enforcement.

If you use is-latest in a multi-team environment or on a tracking plan with multiple Sources it can require extra steps to keep the merge checks in a passing state. Changes merged into to the main tracking plan by other teams or on other Sources cause future runs of is-latest to fail in your project. This is because it's no longer on the latest version.

In this case you can explicitly "lock" the tracking plan to a specific version on the main branch and using --is-latest-if-not-on-default-branch.

This means any time an "external" change happens to the tracking plan you must follow these steps:

  1. Run ampli pull to update the ampli.json to be on the main Branch.
  2. Commit changes to Git branch.

After the ampli.json is on the main branch future calls to --is-latest-if-not-on-default-branch pass. This is useful to keep the current version fixed regardless of new tracking plan changes.

Note

If you run ampli pull on a merged version it will update the ampli.json and Ampli Wrapper to contain the associated version main that was created by the merged branch. If you want to get the latest version of main you can run ampli pull a second time.

GitHub Actions

Initial setup

  1. Add ampli-implementation-check.yml and ampli-merge-check.yml to your .github/workflows directory.

    1name: Ampli Implementation Check
    2on: pull_request
    3 
    4jobs:
    5 build:
    6 runs-on: ubuntu-latest
    7 container:
    8 image: amplitudeinc/ampli
    9 
    10 steps:
    11 - name: Checkout repo
    12 uses: actions/checkout@v2
    13 
    14 - name: Verify analytics implementation and update status in Data
    15 run: ampli status -u --skip-update-on-default-branch -t $
    1name: Ampli Merge Check
    2on: pull_request
    3 
    4jobs:
    5 build:
    6 runs-on: ubuntu-latest
    7 container:
    8 image: amplitudeinc/ampli
    9 
    10 steps:
    11 - name: Checkout repo
    12 uses: actions/checkout@v2
    13 
    14 - name: Check the Data branch is merged before merging the Git branch
    15 run: ampli status --is-merged -t $
  2. If your Ampli project is in a subdirectory, you may need to set the correct working-directory in your Actions. See GitHub documentation here.

  3. Create a API token in Data. Do this from Settings => API Tokens => Create Token.

  4. Add the API token to your Repository secrets as AMPLI_TOKEN. You can do this from
    Settings => Secrets => Actions => New repository secret

  5. Commit the workflows to your repo and you're all set. On each PR Ampli checks both the implementation status and merge status of the current branch in your tracking plan.

PR workflow

  1. During development the "Ampli Merge Check" in GitHub fails continuously until the corresponding branch is merged in Data. This behavior ensures that your code stays in sync with the tracking plan.

  2. Start by implementing the latest changes to your tracking plan. The Ampli Implementation Check verifies this.

  3. After all other checks have passed except the "Ampli Merge Check", you are ready to get approval in GitHub.

  4. After your GitHub PR is approved, you can go to https://data.amplitude.com and merge your Data branch. Do this under Home after selecting the branch in the left-hand dropdown.

    Warning

    If your Data branch is out of date with the main tracking plan you may need to "Refresh" before you can merge. This applies the latest changes from main to your Data branch. If you "Refresh" your branch it's important to also pull the latest changes in both Git and Ampli.

  5. After the branch is merged in Data, you can re-run the "Ampli Merge Check" in GitHub.

    • Click "Details" next to the failed "Ampli Merge Check - PR" job
    • Click "Re-run jobs" => "Re-run failed jobs"
  6. After the "Ampli Merge Check" runs successfully, you can merge your Git branch feeling confident about your analytics implementation.

Bitbucket Pipelines, Circle CI, and other CI/CD systems

You can add merge and implementation checks to many different CI workflows, all it takes is the Ampli CLI. The easiest
way to integrate is to use the Ampli CLI Docker containers which are already
configured to run the Ampli CLI commands.

Follow the high-level setup to add merge and implementation checks to your CI workflows.

High-level setup:

  1. Use a Ampli CLI Docker image to run Ampli CLI commands.
  2. Create an API token in Data.
  3. Add the API token as a repository / workflow secret.
  4. Add Ampli implementation check to run on PR.
  5. Add Ampli merge check to run on PR.

See the GitHub Actions instructions for inspiration.

FAQs

Should the Ampli generated code and the ampli.json file be checked into git?

Yes, the generated code from Ampli as well as ampli.json should be checked into your source control. With the ampli.json file checked in, the source code is always pinned to a certain point in time of the Tracking Plan and the Ampli CLI configuration is automatically shared within your team. The Ampli generated file is also checked in to git just like any other code in your project.

Do I need to manually resolve git conflicts in the Ampli generated code and ampli.json?

No, if you have git conflicts in any Ampli related files (Ampli generated code or ampli.json), run ampli pull and the Ampli CLI takes care of resolving the conflicts automatically.

The ampli.json on my git main branch is pointing to an Ampli branch that has already been merged, is that expected?

Yes, that's expected! The Ampli CLI uses this information to verify that the Ampli branch has been merged, for example when automatically resolving git merge conflicts. It's also a documentation for which version of the Tracking Plan was used for the most recent tracking plan implementation for this source. The Ampli CLI takes care of automatically updating ampli.json on next ampli pull, pinning it to the new Ampli branch you're pulling from.

Do I need to run ampli pull -b main before merging my Git branch?

Not anymore! If you were using an earlier version of Ampli make sure to update to Ampli CLI 1.9.0+ and pull the latest version of your Ampli Wrapper. After that you'll never need to run ampli pull -b main again.

Was this page helpful?

Thanks for your feedback!

May 10th, 2024

Need help? Contact Support

Visit Amplitude.com

Have a look at the Amplitude Blog

Learn more at Amplitude Academy

© 2024 Amplitude, Inc. All rights reserved. Amplitude is a registered trademark of Amplitude, Inc.