deconf / Analytics-Insights

GNU General Public License v2.0
5 stars 7 forks source link

Update AIWP_GAPI_Controller->handle_corereports_ga4() to be public an… #8

Open jerclarke opened 1 year ago

jerclarke commented 1 year ago

This is the smallest change that will let me proceed with a major feature I need from AIWP: The ability to perform custom API queries from outside of AIWP using the plugin's query tools.

Currently the AIWP_GAPI_Controller->handle_corereports_ga4() setup is very messy, with the widget doing a lot of setup and handling in each function that calls it, so if you wanted to make this function easy-to-use, there's a lot more work that could be done, but even without all that, this small change at least makes it possible for me to call AIWP_GAPI_Controller->handle_corereports_ga4() and get results as long as I've done the same setup you would normally do. Obviously if it's a private function, I am locked out and/or would have to make my calling code a lot more complicated to make it work.

I also added a $limit argument because I need it for my queries, and it's a major part of the API that is supported in the Google library, but you happened not to have used it. My main query I'm doing is for "popular posts", and as we have tens of thousands of posts on our site, we need to limit the results. I suspect using the $limit argument might be a good idea for your usage too (the widget on our dashboard shows as having 1000 pages of results to click through...)

This gist contains the code I worked out to use the AIWP query internals to perform custom queries:

https://gist.github.com/jerclarke/fd78006499c345d62201a0f883c8c52a

You can test it by adding /?gv_test_aiwp_queries=1 to the homepage URL of a site that is already set up with AIWP. Most of the code is just a generalized and cleaned up version of your code that calls handle_corereports_ga4().

Ideally I'd just call something like aiwp_get_query_results() and it would all be handled, but I'm fine with using this code as-is, as long as I'm able to call that function myself somehow.

Thanks for considering this change!

If you can think of a better way for me to do it, I am eager to hear your ideas :)

deconf commented 1 year ago

Hi,

I would prefer to keep this method private, the only reason is to avoid exposing it to unproper usage which could generate tons of issues.

We need to find a different way, or in worst case scenario to add a wrapper method, at least to be able to determine from error logs an invalid usage.

jerclarke commented 1 year ago

Yeah, I thought that would be your approach. Happy to have a wrapper, maybe one that incorporates some of the setup from my class, like fetching the projectId and doing the timezone stuff.

Thanks for any time you can put into this 🙏🏻