-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathhm-top-posts-ga.php
161 lines (116 loc) · 4.67 KB
/
hm-top-posts-ga.php
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
<?php
/**
* Get Top Articles by Google Analytics.
*
* Get the results of the query stored for quiery_id. If it doesn't exist, a new one is generated based on $args.
* Retruns an array of $post_id => $page_views.
*
* USAGE
*
* Instantiate class - passing args.
* Call get_top_posts
* Done!
*
* @param (array) $args
*/
class HMTP_Top_Posts {
var $prefix = 'hmtp';
function __construct( $args = array() ) {
$this->profile_id = get_option('hmtp_top_posts_setting_profile_id');
$this->username = get_option('hmtp_top_posts_setting_username');
$this->password = get_option('hmtp_top_posts_setting_password');
$this->args = $args;
$this->parse_args();
}
function parse_args() {
// Convert term names to IDs.
if ( ! empty( $this->args['terms'] ) )
foreach( $this->args['terms'] as &$term )
if ( ! is_numeric( $term ) )
$term = get_term_by( 'name', $term, $this->args['taxonomy'] )->term_id;
$defaults = array(
'count' => 5,
'filter' => null, // gapi filter
'taxonomy' => null, // (string) taxonomy to query by.
'terms' => array(), // array of terms to query by
'start_date' => null, // format YYYY-mm-dd
'end_date' => null, // format YYYY-mm-dd
'post_type' => array( 'post' ), // only supports post & page.
'background_only' => true
);
// If too many results - can filter results using permalink structure.
// 'pagePath =~ ^' . str_replace( '/%postname%', '', get_option('permalink_structure') ) . '.*'
$this->args = wp_parse_args( $this->args, $defaults );
$this->query_id = 'hmtp_' . hash( 'crc32', serialize( array_merge( $this->args, array( $this->profile_id ) ) ) );
}
function get_results() {
if ( class_exists( 'TLC_Transient' ) ) {
if ( $this->args['background_only'] )
$results = tlc_transient( $this->query_id )->expires_in( 86400 )->background_only()->updates_with( array( $this, 'fetch_results' ) )->get();
else
$results = tlc_transient( $this->query_id )->expires_in( 86400 )->updates_with( array( $this, 'fetch_results' ) )->get();
return $results;
} else {
if ( $results = get_transient( $this->query_id ) )
return $results;
$results = $this->fetch_results();
set_transient( $this->query_id, $results, 86400 );
return $results;
}
}
function fetch_results() {
// If not - lets not bother going any further with this shall we.
if ( empty( $this->username ) || empty( $this->password ) )
return array();
try {
$ga = new gapi( $this->username, $this->password );
} catch( Exception $e ) {
update_option( 'hmtp_top_posts_error_message', $e->getMessage() );
return;
}
$dimensions = array( 'pagePath' );
$metrics = array( 'pageviews' );
// Build up a list of top posts.
// Keeps going looping through - 30 results at a time - until there are either enough posts or no more results from GA.
$top_posts = array();
$start_index = 1;
while ( count( $top_posts ) < $this->args['count'] ) {
try {
$ga->requestReportData( $this->profile_id, $dimensions, $metrics, '-pageviews', $this->args['filter'], $this->args['start_date'], null, $start_index, 30 );
} catch( Exception $e ) {
update_option( 'hmtp_top_posts_error_message', $e->getMessage() );
return;
}
$gaResults = $ga->getResults();
if ( empty( $gaResults ) )
break;
foreach ( $gaResults as $result ) {
// Get the post id from the url
// Does not work for custom post types.
$post_id = url_to_postid( str_replace( 'index.htm', '', apply_filters( 'hmtp_result_url', (string) $result ) ) );
// Does this top url even relate to a post at all?
// If your permalink structure clashes with page/category/tag structure it just might.
if ( ! $post_id )
continue;
// This can get confusing if we don't pass explicit post types. Who knows what GA will come up with.
if ( ! in_array( get_post_type( $post_id ), $this->args['post_type'] ) )
continue;
if ( get_post_meta( $post_id, 'hmtp_top_posts_optout', true ) === 'on' )
continue;
// If taxonomy and terms supplied - check if theyre is any intersect between those terms and the post terms.
if ( ! is_null( $this->args['taxonomy'] ) && ! empty( $this->args['terms'] ) && 0 == count( array_intersect( wp_get_object_terms( $post_id, $this->args['taxonomy'], array( 'fields' => 'ids') ), $this->args['terms'] ) ) )
continue;
// Build an array of $post_id => $pageviews
$top_posts[$post_id] = array(
'post_id' => $post_id,
'views' => $result->getPageviews(),
);
// Once we have enough posts we can break out of this.
if ( isset( $top_posts ) && count( $top_posts ) >= $this->args['count'] )
break;
}
$start_index += 30;
}
return $top_posts;
}
}