Skip to content

Instantly share code, notes, and snippets.

@leewillis77
Created June 28, 2017 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leewillis77/c07c22ec791307d2c25b77439495805e to your computer and use it in GitHub Desktop.
Save leewillis77/c07c22ec791307d2c25b77439495805e to your computer and use it in GitHub Desktop.
Subselect instead of view proof-of-concept
diff --git a/includes/class-campaigns-db.php b/includes/class-campaigns-db.php
index bc572401..ee2e9892 100644
--- a/includes/class-campaigns-db.php
+++ b/includes/class-campaigns-db.php
@@ -27,10 +27,21 @@ class Affiliate_WP_Campaigns_DB extends Affiliate_WP_DB {
if( defined( 'AFFILIATE_WP_NETWORK_WIDE' ) && AFFILIATE_WP_NETWORK_WIDE ) {
// Allows a single visits table for the whole network
- $this->table_name = 'affiliate_wp_campaigns';
+ $visits_db = 'affiliate_wp_visits';
} else {
- $this->table_name = $wpdb->prefix . 'affiliate_wp_campaigns';
+ $visits_db = $wpdb->prefix . 'affiliate_wp_visits';
}
+
+ $this->table_name = '(
+ SELECT affiliate_id,
+ campaign,
+ COUNT(url) as visits,
+ COUNT(DISTINCT url) as unique_visits,
+ SUM(IF(referral_id<>0,1,0)) as referrals,
+ ROUND((SUM(IF(referral_id<>0,1,0))/COUNT(url)) * 100, 2) as conversion_rate
+ FROM ' . $visits_db . ' GROUP BY affiliate_id, campaign
+ ) AS summary_stats';
+
$this->primary_key = 'affiliate_id';
$this->version = '1.0';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment