-
Notifications
You must be signed in to change notification settings - Fork 75
fix: Add batching for visitors data loading for Matomo to avoid too-l… #666
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
anuunchin
merged 3 commits into
dlt-hub:master
from
NHNguyen94:fix/665-matomo-visitors-too-long-url-issue
Dec 8, 2025
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -138,7 +138,7 @@ def matomo_visits( | |
| if get_live_event_visitors: | ||
| resource_list.append( | ||
| visits_data_generator | ||
| | get_unique_visitors(client=client, site_id=live_events_site_id) | ||
| | get_unique_visitors_with_chunk(client=client, site_id=live_events_site_id) | ||
| ) | ||
| return resource_list | ||
|
|
||
|
|
@@ -221,3 +221,41 @@ def get_unique_visitors( | |
| ) | ||
| for method_dict in method_data: | ||
| yield method_dict | ||
|
|
||
|
|
||
| @dlt.transformer( | ||
| data_from=get_last_visits, | ||
| write_disposition="merge", | ||
| name="visitors", | ||
| primary_key="visitorId", | ||
| ) | ||
| def get_unique_visitors_with_chunk( | ||
| visits: List[DictStrAny], | ||
| client: MatomoAPIClient, | ||
| site_id: int, | ||
| chunk_size: int = 20, | ||
| ) -> Iterator[TDataItem]: | ||
| """ | ||
| Dlt transformer. Receives information about visits from get_last_visits. | ||
| This version allows batch loading for visitors data, which is to avoid too-long-URL issue | ||
|
|
||
| Args: | ||
| visits (List[DictStrAny]): List of dicts containing information on last visits in the given timeframe. | ||
| client (MatomoAPIClient): Used to make calls to Matomo API. | ||
| site_id (int): Every site in Matomo has a unique id. | ||
| chunk_size (int): Number of visitor IDs to process in each batch. Defaults to 100. | ||
|
||
|
|
||
| Returns: | ||
| Iterator[TDataItem]: Dict containing information about the visitor. | ||
| """ | ||
|
|
||
| visitor_ids = [visit["visitorId"] for visit in visits] | ||
| indexed_visitor_ids = [ | ||
| visitor_ids[i : i + chunk_size] for i in range(0, len(visitor_ids), chunk_size) | ||
| ] | ||
| for visitor_list in indexed_visitor_ids: | ||
| method_data = client.get_visitors_batch( | ||
| visitor_list=visitor_list, site_id=site_id | ||
| ) | ||
| for method_dict in method_data: | ||
| yield method_dict | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
get_unique_visitors_with_chunkis a slightly modified version ofget_unique_visitors, how about we just adjust theget_unique_visitorsinstead of having a new function that looks almost the same?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, I planned to keep that function for anyone still wants to use that for full load (in case they have less data), but I guess they can always adjust the chunk size for their purpose
Note: Already rebased, there is no new update from master branch