PluginProbe
Analytify – Google Analytics Dashboard For WordPress (GA4 analytics tracking) / 1.1.0
Analytify – Google Analytics Dashboard For WordPress (GA4 analytics tracking) v1.1.0
9.1.2 9.1.1 9.1.0 9.0.2 9.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.1.0 1.1.1 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 All 153 releases
wp-analytify / analytify-general.php

analytify-general.php in Analytify – Google Analytics Dashboard For WordPress (GA4 analytics tracking) 1.1.0, at analytify-general.php

190 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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.1');
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 define( 'ANALYTIFY_STORE_URL', 'http://wp-analytify.com' );
21 define( 'ANALYTIFY_PRODUCT_NAME', 'Analytify WordPress Plugin' );
22
23 if (! class_exists( 'Analytify_General_FREE' ) ) {
24
25 class Analytify_General_FREE {
26
27 function __construct() {
28
29 if (! class_exists('Analytify_Google_Client') ) {
30
31 require_once ANALYTIFY_LIB_PATH . 'Google/Client.php';
32 require_once ANALYTIFY_LIB_PATH . 'Google/Service/Analytics.php';
33
34 }
35
36 $this->client = new Analytify_Google_Client();
37 $this->client->setApprovalPrompt( 'force' );
38 $this->client->setAccessType( 'offline' );
39 $this->client->setClientId( get_option('ANALYTIFY_CLIENTID'));
40 $this->client->setClientSecret( get_option('ANALYTIFY_CLIENTSECRET') );
41 $this->client->setRedirectUri( ANALYTIFY_REDIRECT );
42 $this->client->setScopes( ANALYTIFY_SCOPE );
43 $this->client->setDeveloperKey( get_option('ANALYTIFY_DEV_KEY') );
44
45 try{
46
47 $this->service = new Analytify_Google_Service_Analytics( $this->client );
48
49 $this->pa_connect();
50
51 }
52 catch ( Analytify_Google_Service_Exception $e ) {
53
54 }
55
56 }
57
58 /**
59 * Connect with Google Analytics API and get authentication token and save it.
60 */
61
62 public function pa_connect() {
63
64 $ga_google_authtoken = get_option('pa_google_token');
65
66 if (! empty( $ga_google_authtoken )) {
67
68 $this->client->setAccessToken( $ga_google_authtoken );
69 }
70 else{
71
72 $authCode = get_option( 'post_analytics_token' );
73
74 if ( empty( $authCode ) ) return false;
75
76 try {
77
78 $accessToken = $this->client->authenticate( $authCode );
79 }
80 catch ( Exception $e ) {
81 return false;
82 }
83
84 if ( $accessToken ) {
85
86 $this->client->setAccessToken( $accessToken );
87
88 update_option( 'pa_google_token', $accessToken );
89
90 return true;
91 } //$accessToken
92 else {
93
94 return false;
95 }
96 }
97
98 $this->token = json_decode($this->client->getAccessToken());
99
100 return true;
101 }
102
103 /*
104 * This function grabs the data from Google Analytics
105 * For individual posts/pages.
106 */
107 public function pa_get_analytics( $metrics, $startDate, $endDate, $dimensions = false, $sort = false, $filter = false, $limit = false ) {
108
109 try{
110
111 $this->service = new Analytify_Google_Service_Analytics($this->client);
112 $params = array();
113
114 if ($dimensions){
115 $params['dimensions'] = $dimensions;
116 } //$dimensions
117
118 if ($sort){
119 $params['sort'] = $sort;
120 } //$sort
121
122 if ($filter){
123 $params['filters'] = $filter;
124 } //$filter
125
126 if ($limit){
127 $params['max-results'] = $limit;
128 } //$limit
129
130 $profile_id = get_option("pt_webprofile");
131
132 if (!$profile_id){
133 return false;
134 }
135
136 return $this->service->data_ga->get('ga:' . $profile_id, $startDate, $endDate, $metrics, $params);
137 }
138
139 catch ( Analytify_Google_Service_Exception $e ) {
140
141 // Show error message only for logged in users.
142 if ( is_user_logged_in() ) echo $e->getMessage();
143
144 }
145 }
146
147 /*
148 * This function grabs the data from Google Analytics
149 * For dashboard.
150 */
151 public function pa_get_analytics_dashboard( $metrics, $startDate, $endDate, $dimensions = false, $sort = false, $filter = false, $limit = false ) {
152
153 try{
154
155 $this->service = new Analytify_Google_Service_Analytics( $this->client );
156 $params = array();
157
158 if ($dimensions){
159 $params['dimensions'] = $dimensions;
160 }
161 if ($sort){
162 $params['sort'] = $sort;
163 }
164 if ($filter){
165 $params['filters'] = $filter;
166 }
167 if ($limit){
168 $params['max-results'] = $limit;
169 }
170
171 $profile_id = get_option("pt_webprofile_dashboard");
172 if (!$profile_id){
173 return false;
174 }
175
176 return $this->service->data_ga->get('ga:' . $profile_id, $startDate, $endDate, $metrics, $params);
177
178 }
179
180 catch ( Analytify_Google_Service_Exception $e ) {
181
182 // Show error message only for logged in users.
183 if ( is_user_logged_in() ) echo $e->getMessage();
184
185 }
186 }
187
188 }
189 }
190 ?>