Skip to content

Commit

Permalink
try access token
Browse files Browse the repository at this point in the history
  • Loading branch information
Khemarato Bhikkhu committed Aug 29, 2024
1 parent 6cce7b3 commit 5df7b44
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
18 changes: 9 additions & 9 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ jobs:
uses: actions/checkout@v4
with:
fetch-depth: 0
# - name: Google Authentication
# uses: google-github-actions/auth@v2
# id: auth
# with:
# create_credentials_file: 'true'
# project_id: 'obu-web-analytic-1724825889303'
# workload_identity_provider: 'projects/927551777183/locations/global/workloadIdentityPools/github/providers/my-repo'
- name: Google Authentication
uses: google-github-actions/auth@v2
id: auth
with:
token_format: "access_token"
project_id: 'obu-web-analytic-1724825889303'
service_account: "analyst@obu-web-analytic-1724825889303.iam.gserviceaccount.com"
workload_identity_provider: 'projects/927551777183/locations/global/workloadIdentityPools/github/providers/my-repo'
- name: Install requirements.txt
run: pip install -r requirements.txt
- name: Build the site
run: |
mkdir -p ${{ env.BUILD_DIR }}
cp -r public/* ${{ env.BUILD_DIR }}/
printf '${{ secrets.SERVICE_ACCOUNT_KEY }}' > /home/runner/credentials.json
GOOGLE_APPLICATION_CREDENTIALS='/home/runner/credentials.json' GA4_PROPERTY_ID=${{ secrets.GA4_PROPERTY_ID }} python build.py --dest=${{ env.BUILD_DIR }}
GA4_ACCESS_TOKEN='${{ steps.auth.outputs.access_token }}' GA4_PROPERTY_ID=${{ secrets.GA4_PROPERTY_ID }} python build.py --dest=${{ env.BUILD_DIR }}
- name: Upload Build Artifact
uses: actions/upload-pages-artifact@v3
with:
Expand Down
8 changes: 7 additions & 1 deletion ga4.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def get_default_property_id():

try:
from google.analytics import data_v1beta
from google.oauth2.credentials import Credentials
from google.auth.exceptions import DefaultCredentialsError
from google.analytics.data_v1beta.types import (
DateRange,
Expand All @@ -27,7 +28,12 @@ def get_default_property_id():
quit(1)

try:
client = data_v1beta.BetaAnalyticsDataClient()
if os.environ.get("GA4_ACCESS_TOKEN"):
client = data_v1beta.BetaAnalyticsDataClient(
credentials=Credentials(token=os.environ['GA4_ACCESS_TOKEN']),
)
else:
client = data_v1beta.BetaAnalyticsDataClient()
except DefaultCredentialsError:
print("""Please set the GOOGLE_APPLICATION_CREDENTIALS environment variable
to the path of your service account private key json file.""")
Expand Down

0 comments on commit 5df7b44

Please sign in to comment.