Automattic / republication-tracker-tool

Provides a Creative Commons sharing button and tracking mechanism that integrates with Google Analytics to track republished articles from external websites.
https://wordpress.org/plugins/republication-tracker-tool/
GNU General Public License v2.0
8 stars 8 forks source link

Can we batch requests to GA? #78

Open benlk opened 4 years ago

benlk commented 4 years ago

Rather than firing a new GA request every single time the tracking image is called, can we instead batch those requests to, say, once per minute or once per hour?

We'd need to have a way to temporarily track the hit-tracking information in the database, though, and because it's something we need to keep, we can't use the Transients API to do so, since the Transients API says we can't assume that the data in the transient will exist. https://codex.wordpress.org/Transients_API

joshdarby commented 4 years ago

I wonder if we could just expand on this functionality that saves each hit to the relevant post

https://github.com/INN/republication-tracker-tool/blob/4775fa7d7f63456c82b6e5ecff36ab07f4bfd05f/includes/pixel.php#L65-L77

We could use that and every minute/hour/x fire a job that fires off the request to GA.

benlk commented 4 years ago

We're sending more than the bare number of hits to GA, however:

https://github.com/INN/republication-tracker-tool/blob/4775fa7d7f63456c82b6e5ecff36ab07f4bfd05f/includes/pixel.php#L79-L106

Could we write a function for https://codex.wordpress.org/Function_Reference/wp_schedule_single_event ? That would at least get the HTTP ping loop out of the reply to the visitor.

Based on https://developer.wordpress.org/reference/classes/WP_Http/request/ it looks like we may be able to pass a 'blocking' => false argument as part of the wp_remote_post( $url, $args ) args array, so that the server doesn't wait for the response contents.