-
Notifications
You must be signed in to change notification settings - Fork 41
52 lines (44 loc) · 1.78 KB
/
push_to_posthog.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
name: Repository Traffic to PostHog
on:
schedule:
- cron: '0 0 * * *' # Run daily at midnight
workflow_dispatch:
jobs:
send_traffic_data:
runs-on: ubuntu-latest
permissions: write-all
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",
"api_key": "$POSTHOG_API_KEY",
"distinct_id": "github_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"