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