Skip to content

Commit

Permalink
Added posthog action
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrewster committed Apr 19, 2024
1 parent e9a7975 commit 8f8790c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 63 deletions.
60 changes: 17 additions & 43 deletions .github/workflows/publish_docker_usage.yml
Original file line number Diff line number Diff line change
@@ -1,49 +1,23 @@
name: Repository Traffic to PostHog

name: Build and Push Usage Service Docker Images
on:
schedule:
- cron: '0 0 * * *' # Run daily at midnight
workflow_run:
workflows: [ "Bump Usage Service Version" ]
types:
- completed
workflow_dispatch:

jobs:
send_traffic_data:
docker_push_chatbot:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Get repository traffic data and send to PostHog
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
POSTHOG_API_KEY: ${{ secrets.POSTHOG_API_KEY }}
run: |
# Get the repository owner and name
OWNER=$(echo "${{ github.repository }}" | cut -d'/' -f1)
REPO=$(echo "${{ github.repository }}" | cut -d'/' -f2)
# Get the clone traffic data
CLONES=$(gh api -H "Accept: application/vnd.github+json" /repos/$OWNER/$REPO/traffic/clones | jq '.count')
UNIQUE_CLONES=$(gh api -H "Accept: application/vnd.github+json" /repos/$OWNER/$REPO/traffic/clones | jq '.uniques')
# Get the view traffic data
VIEWS=$(gh api -H "Accept: application/vnd.github+json" /repos/$OWNER/$REPO/traffic/views | jq '.count')
UNIQUE_VIEWS=$(gh api -H "Accept: application/vnd.github+json" /repos/$OWNER/$REPO/traffic/views | jq '.uniques')
# Prepare the payload for PostHog
PAYLOAD=$(cat <<EOF
{
"event": "repository_traffic",
"properties": {
"clones": $CLONES,
"unique_clones": $UNIQUE_CLONES,
"views": $VIEWS,
"unique_views": $UNIQUE_VIEWS,
"repository": "${{ github.repository }}"
}
}
EOF
)
# Send the payload to PostHog
curl -X POST -H "Content-Type: application/json" -d "$PAYLOAD" "https://app.posthog.com/capture?api_key=${{ secrets.POSTHOG_API_KEY }}"
- name: Checkout
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- run: make chatbot_push
working-directory: usage-service/usage-server
38 changes: 18 additions & 20 deletions .github/workflows/push_to_posthog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v3

- name: Get repository traffic data
id: traffic_data
- name: Get repository traffic data and send to PostHog
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
POSTHOG_API_KEY: ${{ secrets.POSTHOG_API_KEY }}
run: |
# Get the repository owner and name
OWNER=$(echo "${{ github.repository }}" | cut -d'/' -f1)
Expand All @@ -30,22 +30,20 @@ jobs:
VIEWS=$(gh api -H "Accept: application/vnd.github+json" /repos/$OWNER/$REPO/traffic/views | jq '.count')
UNIQUE_VIEWS=$(gh api -H "Accept: application/vnd.github+json" /repos/$OWNER/$REPO/traffic/views | jq '.uniques')
# Set the traffic data as output variables
echo "::set-output name=clones::$CLONES"
echo "::set-output name=unique_clones::$UNIQUE_CLONES"
echo "::set-output name=views::$VIEWS"
echo "::set-output name=unique_views::$UNIQUE_VIEWS"
- name: Send traffic data to PostHog
uses: actions/posthog-github-action
with:
api_key: ${{ secrets.POSTHOG_API_KEY }}
event: repository_traffic
properties: |
{
"clones": "${{ steps.traffic_data.outputs.clones }}",
"unique_clones": "${{ steps.traffic_data.outputs.unique_clones }}",
"views": "${{ steps.traffic_data.outputs.views }}",
"unique_views": "${{ steps.traffic_data.outputs.unique_views }}",
# Prepare the payload for PostHog
PAYLOAD=$(cat <<EOF
{
"event": "repository_traffic",
"properties": {
"clones": $CLONES,
"unique_clones": $UNIQUE_CLONES,
"views": $VIEWS,
"unique_views": $UNIQUE_VIEWS,
"repository": "${{ github.repository }}"
}
}
}
EOF
)
# Send the payload to PostHog
curl -X POST -H "Authorization: Bearer ${{ secrets.POSTHOG_API_KEY }}" -H "Content-Type: application/json" -d "$PAYLOAD" "https://app.posthog.com/capture"

0 comments on commit 8f8790c

Please sign in to comment.