Created
April 5, 2024 10:15
-
-
Save norberttech/1316079dd64201fc7781d1addc95caac to your computer and use it in GitHub Desktop.
Fully functional example based on https://flow-php.com/blog/2024-04-04/building-custom-extractor-google-analytics
This file contains 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
<?php | |
$adapter = new PSRSimpleCache( | |
new Psr16Cache(new FilesystemAdapter(defaultLifetime: 3600, directory: __DIR__ . '/cache')) | |
); | |
data_frame(config_builder()->cache($adapter)) | |
->read( | |
from_cache( | |
id: 'ga_account_summaries', | |
fallback_extractor: from_ga_account_summaries($client) | |
) | |
) | |
->cache('ga_account_summaries') | |
->collect() | |
->join( | |
data_frame(config_builder()->cache($adapter)) | |
->read( | |
from_cache( | |
id: 'ga_accounts', | |
fallback_extractor: from_ga_accounts($client) | |
) | |
) | |
->cache('ga_accounts') | |
->rename('name', 'account') | |
->collect(), | |
join_on(equal('account', 'account'), 'account_') | |
) | |
->drop('account_display_name', 'name') | |
->rename('account', 'account_name') | |
->rename('display_name', 'account_display_name') | |
->drop('property_summaries') | |
->joinEach( | |
new readonly class($client) implements DataFrameFactory { | |
public function __construct(private AnalyticsAdminServiceClient $client) | |
{ | |
} | |
public function from(Rows $rows): DataFrame | |
{ | |
return data_frame() | |
->read(from_ga_account_properties($this->client, $rows->reduceToArray(ref('account_name')))) | |
->rename('account', 'account_name'); | |
} | |
}, | |
join_on(equal('account_name', 'account_name'), 'property_') | |
) | |
->drop('property_parent') | |
->withEntry('account_type', when(ref('property_name')->isNull(), lit(AccountType::UNIVERSAL_ANALYTICS), lit(AccountType::GA4))) | |
->write(to_output(false, output: Output::rows_and_schema)) | |
->run(); |
This file contains 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
+---------------------+---------------------------+---------------------------+--------------------+-----------------------+----------------------+------------------------------+------------------------+---------------------------+---------------------------+----------------------+----------------------+-------------------------------+---------------------+------------------------+---------------------------+---------------------+ | |
| account_region_code | account_create_time | account_update_time | account_name | account_display_name | property_name | property_display_name | property_property_type | property_create_time | property_update_time | property_delete_time | property_expire_time | property_industry_category | property_time_zone | property_currency_code | property_service_level | account_type | | |
+---------------------+---------------------------+---------------------------+--------------------+-----------------------+----------------------+------------------------------+------------------------+---------------------------+---------------------------+----------------------+----------------------+-------------------------------+---------------------+------------------------+---------------------------+---------------------+ | |
| PL | 2019-12-01T14:38:46+00:00 | 2019-12-01T14:38:46+00:00 | accounts/123123123 | account_01 | properties/123123123 | norbert.tech - GA4 | PROPERTY_TYPE_ORDINARY | 2023-04-11T11:06:28+00:00 | 2023-04-11T11:06:28+00:00 | | | TECHNOLOGY | Europe/Warsaw | USD | GOOGLE_ANALYTICS_STANDARD | GA4 | | |
| PL | 2020-06-19T18:56:08+00:00 | 2020-06-19T18:56:08+00:00 | accounts/123123123 | account_02 | properties/123123123 | Forum Aeon PHP | PROPERTY_TYPE_ORDINARY | 2020-11-21T19:46:45+00:00 | 2020-11-21T19:46:45+00:00 | | | TECHNOLOGY | America/Los_Angeles | USD | GOOGLE_ANALYTICS_STANDARD | GA4 | | |
| PL | 2012-01-05T14:19:29+00:00 | 2015-08-10T17:52:19+00:00 | accounts/12312312 | account_03 | properties/123123123 | Softin - GA4 | PROPERTY_TYPE_ORDINARY | 2023-04-13T12:08:37+00:00 | 2023-04-13T12:08:37+00:00 | | | INDUSTRY_CATEGORY_UNSPECIFIED | Europe/Warsaw | USD | GOOGLE_ANALYTICS_STANDARD | GA4 | | |
| PL | 2024-02-04T15:59:25+00:00 | 2024-02-04T15:59:25+00:00 | accounts/123123123 | account_04 | properties/123123123 | flow-php.com | PROPERTY_TYPE_ORDINARY | 2024-02-04T15:59:25+00:00 | 2024-02-04T15:59:25+00:00 | | | TECHNOLOGY | Europe/Warsaw | USD | GOOGLE_ANALYTICS_STANDARD | GA4 | | |
| PL | 2012-09-28T15:35:38+00:00 | 2015-08-11T20:34:06+00:00 | accounts/12312312 | account_05 | | | | | | | | | | | | UNIVERSAL_ANALYTICS | | |
| PL | 2013-01-14T21:04:24+00:00 | 2015-08-10T22:18:11+00:00 | accounts/12312312 | account_06 | | | | | | | | | | | | UNIVERSAL_ANALYTICS | | |
| PL | 2015-09-25T11:14:00+00:00 | 2015-09-25T11:14:00+00:00 | accounts/12312312 | account_07 | properties/123123123 | zawarstwaabstrakcji.pl - GA4 | PROPERTY_TYPE_ORDINARY | 2023-04-20T23:36:10+00:00 | 2023-04-20T23:36:10+00:00 | | | TECHNOLOGY | Europe/Warsaw | USD | GOOGLE_ANALYTICS_STANDARD | GA4 | | |
+---------------------+---------------------------+---------------------------+--------------------+-----------------------+----------------------+------------------------------+------------------------+---------------------------+---------------------------+----------------------+----------------------+-------------------------------+---------------------+------------------------+---------------------------+---------------------+ | |
7 rows | |
schema | |
|-- account_region_code: string | |
|-- account_create_time: datetime | |
|-- account_update_time: datetime | |
|-- account_name: string | |
|-- account_display_name: string | |
|-- property_name: ?string | |
|-- property_display_name: ?string | |
|-- property_property_type: ?string | |
|-- property_create_time: ?string | |
|-- property_update_time: ?string | |
|-- property_delete_time: ?string | |
|-- property_expire_time: ?string | |
|-- property_industry_category: ?string | |
|-- property_time_zone: ?string | |
|-- property_currency_code: ?string | |
|-- property_service_level: ?string | |
|-- account_type: enum<AccountType> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment