| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Base Class to use for the Add-ons |
| 5 |
* It will be used to extend the functionality of Analytify WordPress Plugin. |
| 6 |
*/ |
| 7 |
|
| 8 |
// Setting Global Values |
| 9 |
define( 'ANALYTIFY_LIB_PATH', dirname(__FILE__) . '/lib/' ); |
| 10 |
define( 'ANALYTIFY_ID', 'wp-analytify-options' ); |
| 11 |
define( 'ANALYTIFY_NICK', 'Analytify' ); |
| 12 |
define( 'ANALYTIFY_ROOT_PATH', dirname(__FILE__) ); |
| 13 |
define( 'ANALYTIFY_VERSION', '1.1.2'); |
| 14 |
define( 'ANALYTIFY_TYPE', 'FREE'); |
| 15 |
define( 'ANALYTIFY_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); |
| 16 |
define( 'ANALYTIFY_CLIENTID', '958799092305-7p6jlsnmv1dn44a03ma00kmdrau2i31q.apps.googleusercontent.com' ); |
| 17 |
define( 'ANALYTIFY_CLIENTSECRET', 'Mzs1ODgJTpjk8mzQ3mbrypD3' ); |
| 18 |
define( 'ANALYTIFY_REDIRECT', 'https://wp-analytify.com/api/' ); |
| 19 |
define( 'ANALYTIFY_DEV_KEY', 'AIzaSyAn-70Vah_wB9qifJqjrOhkl77qzWhAR_w'); |
| 20 |
define( 'ANALYTIFY_SCOPE', 'https://www.googleapis.com/auth/analytics' ); // readonly scope |
| 21 |
define( 'ANALYTIFY_STORE_URL', 'http://wp-analytify.com' ); |
| 22 |
define( 'ANALYTIFY_PRODUCT_NAME', 'Analytify WordPress Plugin' ); |
| 23 |
|
| 24 |
if (! class_exists( 'Analytify_General_FREE' ) ) { |
| 25 |
|
| 26 |
class Analytify_General_FREE { |
| 27 |
|
| 28 |
function __construct() { |
| 29 |
|
| 30 |
if (! class_exists('Analytify_Google_Client') ) { |
| 31 |
|
| 32 |
require_once ANALYTIFY_LIB_PATH . 'Google/Client.php'; |
| 33 |
require_once ANALYTIFY_LIB_PATH . 'Google/Service/Analytics.php'; |
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
$this->client = new Analytify_Google_Client(); |
| 38 |
$this->client->setApprovalPrompt( 'force' ); |
| 39 |
$this->client->setAccessType( 'offline' ); |
| 40 |
|
| 41 |
if( get_option( 'ANALYTIFY_USER_KEYS') == 'Yes' ) { |
| 42 |
|
| 43 |
$this->client->setClientId( get_option('ANALYTIFY_CLIENTID')); |
| 44 |
$this->client->setClientSecret( get_option('ANALYTIFY_CLIENTSECRET') ); |
| 45 |
$this->client->setRedirectUri( get_option('ANALYTIFY_REDIRECT_URI') ); |
| 46 |
$this->client->setDeveloperKey( get_option('ANALYTIFY_DEV_KEY') ); |
| 47 |
|
| 48 |
}else{ |
| 49 |
|
| 50 |
$this->client->setClientId( ANALYTIFY_CLIENTID ); |
| 51 |
$this->client->setClientSecret( ANALYTIFY_CLIENTSECRET ); |
| 52 |
$this->client->setRedirectUri( ANALYTIFY_REDIRECT ); |
| 53 |
$this->client->setDeveloperKey( ANALYTIFY_DEV_KEY ); |
| 54 |
|
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
$this->client->setScopes( ANALYTIFY_SCOPE ); |
| 59 |
|
| 60 |
try{ |
| 61 |
|
| 62 |
$this->service = new Analytify_Google_Service_Analytics( $this->client ); |
| 63 |
|
| 64 |
$this->pa_connect(); |
| 65 |
|
| 66 |
} |
| 67 |
catch ( Analytify_Google_Service_Exception $e ) { |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Connect with Google Analytics API and get authentication token and save it. |
| 75 |
*/ |
| 76 |
|
| 77 |
public function pa_connect() { |
| 78 |
|
| 79 |
|
| 80 |
$ga_google_authtoken = get_option('pa_google_token'); |
| 81 |
|
| 82 |
if (! empty( $ga_google_authtoken )) { |
| 83 |
|
| 84 |
$this->client->setAccessToken( $ga_google_authtoken ); |
| 85 |
} |
| 86 |
else{ |
| 87 |
|
| 88 |
$authCode = get_option( 'post_analytics_token' ); |
| 89 |
|
| 90 |
if ( empty( $authCode ) ) return false; |
| 91 |
|
| 92 |
try { |
| 93 |
|
| 94 |
$accessToken = $this->client->authenticate( $authCode ); |
| 95 |
} |
| 96 |
catch ( Exception $e ) { |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
if ( $accessToken ) { |
| 101 |
|
| 102 |
$this->client->setAccessToken( $accessToken ); |
| 103 |
|
| 104 |
update_option( 'pa_google_token', $accessToken ); |
| 105 |
|
| 106 |
return true; |
| 107 |
} //$accessToken |
| 108 |
else { |
| 109 |
|
| 110 |
return false; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
$this->token = json_decode($this->client->getAccessToken()); |
| 115 |
|
| 116 |
return true; |
| 117 |
} |
| 118 |
|
| 119 |
/* |
| 120 |
* This function grabs the data from Google Analytics |
| 121 |
* For individual posts/pages. |
| 122 |
*/ |
| 123 |
public function pa_get_analytics( $metrics, $startDate, $endDate, $dimensions = false, $sort = false, $filter = false, $limit = false ) { |
| 124 |
|
| 125 |
try{ |
| 126 |
|
| 127 |
$this->service = new Analytify_Google_Service_Analytics($this->client); |
| 128 |
$params = array(); |
| 129 |
|
| 130 |
if ($dimensions){ |
| 131 |
$params['dimensions'] = $dimensions; |
| 132 |
} //$dimensions |
| 133 |
|
| 134 |
if ($sort){ |
| 135 |
$params['sort'] = $sort; |
| 136 |
} //$sort |
| 137 |
|
| 138 |
if ($filter){ |
| 139 |
$params['filters'] = $filter; |
| 140 |
} //$filter |
| 141 |
|
| 142 |
if ($limit){ |
| 143 |
$params['max-results'] = $limit; |
| 144 |
} //$limit |
| 145 |
|
| 146 |
$profile_id = get_option("pt_webprofile"); |
| 147 |
|
| 148 |
if (!$profile_id){ |
| 149 |
return false; |
| 150 |
} |
| 151 |
|
| 152 |
return $this->service->data_ga->get('ga:' . $profile_id, $startDate, $endDate, $metrics, $params); |
| 153 |
} |
| 154 |
|
| 155 |
catch ( Analytify_Google_Service_Exception $e ) { |
| 156 |
|
| 157 |
// Show error message only for logged in users. |
| 158 |
if ( is_user_logged_in() ) echo $e->getMessage(); |
| 159 |
|
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
/* |
| 164 |
* This function grabs the data from Google Analytics |
| 165 |
* For dashboard. |
| 166 |
*/ |
| 167 |
public function pa_get_analytics_dashboard( $metrics, $startDate, $endDate, $dimensions = false, $sort = false, $filter = false, $limit = false ) { |
| 168 |
|
| 169 |
try{ |
| 170 |
|
| 171 |
$this->service = new Analytify_Google_Service_Analytics( $this->client ); |
| 172 |
$params = array(); |
| 173 |
|
| 174 |
if ($dimensions){ |
| 175 |
$params['dimensions'] = $dimensions; |
| 176 |
} |
| 177 |
if ($sort){ |
| 178 |
$params['sort'] = $sort; |
| 179 |
} |
| 180 |
if ($filter){ |
| 181 |
$params['filters'] = $filter; |
| 182 |
} |
| 183 |
if ($limit){ |
| 184 |
$params['max-results'] = $limit; |
| 185 |
} |
| 186 |
|
| 187 |
$profile_id = get_option("pt_webprofile_dashboard"); |
| 188 |
if (!$profile_id){ |
| 189 |
return false; |
| 190 |
} |
| 191 |
|
| 192 |
return $this->service->data_ga->get('ga:' . $profile_id, $startDate, $endDate, $metrics, $params); |
| 193 |
|
| 194 |
} |
| 195 |
|
| 196 |
catch ( Analytify_Google_Service_Exception $e ) { |
| 197 |
|
| 198 |
// Show error message only for logged in users. |
| 199 |
if ( is_user_logged_in() ) echo $e->getMessage(); |
| 200 |
|
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
} |
| 205 |
} |
| 206 |
?> |