Skip to content

add base64 credentials option #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion docs/supported-sources/google_analytics.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ The URI format for Google Analytics is as follows:
```plaintext
googleanalytics://?credentials_path=/path/to/service/account.json&property_id=<property_id>
```
Alternatively, you can use base64 encoded credentials:

```
googleanalytics://?credentials_base64=<base64_encoded_credentials>&property_id=<property_id>
```

URI parameters:
- `credentials_path`: The path to the service account JSON file.
- `property_id`: It is a unique number that identifies a particular property on Google Analytics. [Follow this guide](https://developers.google.com/analytics/devguides/reporting/data/v1/property-id#what_is_my_property_id) if you don't know your property ID.
- `property_id`: It is a unique number that identifies a particular property on Google Analytics. [Follow this guide](https://developers.google.com/analytics/devguides/reporting/data/v1/property-id#what_is_my_property_id) to know more about property ID.

## Setting up an Google Analytics Integration
Google Analytics requires a few steps to set up an integration, please follow the guide dltHub [has built here](https://dlthub.com/docs/dlt-ecosystem/verified-sources/google_analytics#grab-google-service-account-credentials). Once you complete the guide, you should have an `.json` file and `project_id`.
Expand Down
16 changes: 11 additions & 5 deletions ingestr/src/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -1398,13 +1398,19 @@ def dlt_source(self, uri: str, table: str, **kwargs):
parse_uri = urlparse(uri)
source_fields = parse_qs(parse_uri.query)
cred_path = source_fields.get("credentials_path")
cred_base64 = source_fields.get("credentials_base64")

if not cred_path:
raise ValueError("credentials_path is required to connect Google Analytics")
credentials = {}
if not cred_path and not cred_base64:
raise ValueError("credentials_path or credentials_base64 is required to connect Google Analytics")

with open(cred_path[0], "r") as f:
credentials = json.load(f)
credentials = {}
if cred_path:
with open(cred_path[0], "r") as f:
credentials = json.load(f)
elif cred_base64:
credentials = json.loads(
base64.b64decode(cred_base64[0]).decode("utf-8")
)

property_id = source_fields.get("property_id")
if not property_id:
Expand Down