| 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.0'); |
| 14 |
define( 'ANALYTIFY_TYPE', 'FREE'); |
| 15 |
define( 'ANALYTIFY_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); |
| 16 |
|
| 17 |
define( 'ANALYTIFY_REDIRECT', 'urn:ietf:wg:oauth:2.0:oob' ); // This will redirect to window where we can copy Access code. |
| 18 |
define( 'ANALYTIFY_SCOPE', 'https://www.googleapis.com/auth/analytics' ); // readonly scope |
| 19 |
|
| 20 |
|
| 21 |
define( 'ANALYTIFY_STORE_URL', 'http://wp-analytify.com' ); |
| 22 |
define( 'ANALYTIFY_PRODUCT_NAME', 'Analytify WordPress Plugin' ); |
| 23 |
|
| 24 |
if (! class_exists( 'Analytify_General' ) ) { |
| 25 |
|
| 26 |
class Analytify_General { |
| 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 |
$this->client->setClientId( get_option('ANALYTIFY_CLIENTID')); |
| 41 |
$this->client->setClientSecret( get_option('ANALYTIFY_CLIENTSECRET') ); |
| 42 |
$this->client->setRedirectUri( ANALYTIFY_REDIRECT ); |
| 43 |
$this->client->setScopes( ANALYTIFY_SCOPE ); |
| 44 |
$this->client->setDeveloperKey( get_option('ANALYTIFY_DEV_KEY') ); |
| 45 |
|
| 46 |
try{ |
| 47 |
|
| 48 |
$this->service = new Analytify_Google_Service_Analytics( $this->client ); |
| 49 |
|
| 50 |
$this->pa_connect(); |
| 51 |
|
| 52 |
} |
| 53 |
catch ( Analytify_Google_Service_Exception $e ) { |
| 54 |
|
| 55 |
} |
| 56 |
|
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Connect with Google Analytics API and get authentication token and save it. |
| 61 |
*/ |
| 62 |
|
| 63 |
public function pa_connect() { |
| 64 |
|
| 65 |
$ga_google_authtoken = get_option('pa_google_token'); |
| 66 |
|
| 67 |
if (! empty( $ga_google_authtoken )) { |
| 68 |
|
| 69 |
$this->client->setAccessToken( $ga_google_authtoken ); |
| 70 |
} |
| 71 |
else{ |
| 72 |
|
| 73 |
$authCode = get_option( 'post_analytics_token' ); |
| 74 |
|
| 75 |
if ( empty( $authCode ) ) return false; |
| 76 |
|
| 77 |
try { |
| 78 |
|
| 79 |
$accessToken = $this->client->authenticate( $authCode ); |
| 80 |
} |
| 81 |
catch ( Exception $e ) { |
| 82 |
return false; |
| 83 |
} |
| 84 |
|
| 85 |
if ( $accessToken ) { |
| 86 |
|
| 87 |
$this->client->setAccessToken( $accessToken ); |
| 88 |
|
| 89 |
update_option( 'pa_google_token', $accessToken ); |
| 90 |
|
| 91 |
return true; |
| 92 |
} //$accessToken |
| 93 |
else { |
| 94 |
|
| 95 |
return false; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
$this->token = json_decode($this->client->getAccessToken()); |
| 100 |
|
| 101 |
return true; |
| 102 |
} |
| 103 |
|
| 104 |
/* |
| 105 |
* This function grabs the data from Google Analytics |
| 106 |
* For individual posts/pages. |
| 107 |
*/ |
| 108 |
public function pa_get_analytics( $metrics, $startDate, $endDate, $dimensions = false, $sort = false, $filter = false, $limit = false ) { |
| 109 |
|
| 110 |
try{ |
| 111 |
|
| 112 |
$this->service = new Analytify_Google_Service_Analytics($this->client); |
| 113 |
$params = array(); |
| 114 |
|
| 115 |
if ($dimensions){ |
| 116 |
$params['dimensions'] = $dimensions; |
| 117 |
} //$dimensions |
| 118 |
|
| 119 |
if ($sort){ |
| 120 |
$params['sort'] = $sort; |
| 121 |
} //$sort |
| 122 |
|
| 123 |
if ($filter){ |
| 124 |
$params['filters'] = $filter; |
| 125 |
} //$filter |
| 126 |
|
| 127 |
if ($limit){ |
| 128 |
$params['max-results'] = $limit; |
| 129 |
} //$limit |
| 130 |
|
| 131 |
$profile_id = get_option("pt_webprofile"); |
| 132 |
|
| 133 |
if (!$profile_id){ |
| 134 |
return false; |
| 135 |
} |
| 136 |
|
| 137 |
return $this->service->data_ga->get('ga:' . $profile_id, $startDate, $endDate, $metrics, $params); |
| 138 |
} |
| 139 |
|
| 140 |
catch ( Analytify_Google_Service_Exception $e ) { |
| 141 |
|
| 142 |
// Show error message only for logged in users. |
| 143 |
if ( is_user_logged_in() ) echo $e->getMessage(); |
| 144 |
|
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
/* |
| 149 |
* This function grabs the data from Google Analytics |
| 150 |
* For dashboard. |
| 151 |
*/ |
| 152 |
public function pa_get_analytics_dashboard( $metrics, $startDate, $endDate, $dimensions = false, $sort = false, $filter = false, $limit = false ) { |
| 153 |
|
| 154 |
try{ |
| 155 |
|
| 156 |
$this->service = new Analytify_Google_Service_Analytics( $this->client ); |
| 157 |
$params = array(); |
| 158 |
|
| 159 |
if ($dimensions){ |
| 160 |
$params['dimensions'] = $dimensions; |
| 161 |
} |
| 162 |
if ($sort){ |
| 163 |
$params['sort'] = $sort; |
| 164 |
} |
| 165 |
if ($filter){ |
| 166 |
$params['filters'] = $filter; |
| 167 |
} |
| 168 |
if ($limit){ |
| 169 |
$params['max-results'] = $limit; |
| 170 |
} |
| 171 |
|
| 172 |
$profile_id = get_option("pt_webprofile_dashboard"); |
| 173 |
if (!$profile_id){ |
| 174 |
return false; |
| 175 |
} |
| 176 |
|
| 177 |
return $this->service->data_ga->get('ga:' . $profile_id, $startDate, $endDate, $metrics, $params); |
| 178 |
|
| 179 |
} |
| 180 |
|
| 181 |
catch ( Analytify_Google_Service_Exception $e ) { |
| 182 |
|
| 183 |
// Show error message only for logged in users. |
| 184 |
if ( is_user_logged_in() ) echo $e->getMessage(); |
| 185 |
|
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
} |
| 190 |
} |
| 191 |
?> |