This workflow requires Ampli CLI 1.9.0+
This page covers how you can use branches in Ampli with source control systems such as Git.
When you need to update the tracking plan:
ampli pull
.ampli status
.ampli status --is-merged
.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-name10✔ Pulling 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 checkout2? Select a branch3 feature-branch-name (current)4 add-products-to-cart5❯ remove-old-forms6 new-registration-metrics7(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-name2Source: browser3Runtime: Browser/TypeScript4Branch: feature-branch-name5✔ Pulling version 74 (latest)6All events are up to date.
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}
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.
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.
1b1 1 -> 2 -> 32 / \3main 1 -> 2 -> 34 \ \ /5b2 \ 1 -> 2 -> 3 -> 4 -> 56 \7b3 1 -> 2 -> 3 -> 4
ampli status --is-merged
1ampli status --is-merged2✔ Verifying event tracking implementation in source code3 ✔ Event A (1 location)4 ✔ Event B (1 location)5✔ All events tracked 2 found, 2 total6⚠ WARNING 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.8✔ Branch 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
.
ampli status --is-latest
1ampli status --is-latest2✔ Verifying event tracking implementation in source code3 ✔ Event A (1 location)4 ✔ Event B (1 location)5✔ All events tracked 2 found, 2 total6✔ You 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
.
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
.
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.
1ampli status --is-merged --is-latest2✘ Verifying event tracking implementation in source code3 ✘ Event A4 ✘ Event B5✘ ERROR Event tracking incomplete 2 missed, 2 total6✘ ERROR Branch feature-branch-name version 73 is not merged.7✘ ERROR 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.
is-merged
errorThis means you are currently on an un-merged Ampli branch. To fix this error:
ampli pull
again tois-latest
errorampli pull
to update your local project.ampli status --is-merged --is-latest
1ampli status --is-merged --is-latest2✔ Verifying event tracking implementation in source code3 ✔ Event A (1 location)4 ✔ Event B (1 location)5✔ All events tracked 2 found, 2 total6⚠ WARNING 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.8✔ Branch feature-branch-name version 74 is merged.9✔ You 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
.
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.
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.
--skip-update-on-default-branch
when calling --update
or -u
.is-latest
in CIAlthough 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:
ampli pull
to update the ampli.json
to be on the main
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.
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.
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 repo12 uses: actions/checkout@v213 14 - name: Verify analytics implementation and update status in Data15 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 repo12 uses: actions/checkout@v213 14 - name: Check the Data branch is merged before merging the Git branch15 run: ampli status --is-merged -t $
If your Ampli project is in a subdirectory, you may need to set the correct working-directory in your Actions. See GitHub documentation here.
Create a API token in Data. Do this from Settings => API Tokens => Create Token
.
Add the API token to your Repository secrets as AMPLI_TOKEN
. You can do this from
Settings => Secrets => Actions => New repository secret
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.
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.
Start by implementing the latest changes to your tracking plan. The Ampli Implementation Check verifies this.
After all other checks have passed except the "Ampli Merge Check", you are ready to get approval in GitHub.
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.
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.
After the branch is merged in Data, you can re-run the "Ampli Merge Check" in GitHub.
After the "Ampli Merge Check" runs successfully, you can merge your Git branch feeling confident about your analytics implementation.
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:
See the GitHub Actions instructions for inspiration.
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.
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.
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.
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.
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.