/
content_grouping.php
125 lines (96 loc) · 5.21 KB
/
content_grouping.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
<?php
/**
* Send category information from WordPress to Google Analytics (GA).
*
* The WordPress category is passed using the Content Grouping feature of
* GA, to the contentGroup1 group.
*
* If the function is_product() is defined, the script assumes the website
* has an ecommerce, and the product category will be transmitted from the
* 'product_cat' taxonomy to the contentGroup2 group.
*
* This script should be placed just before the pageview is transmitted
* to GA, ie. before the ga ('send', 'pageview') in the standard tracking
* code, lest the grouping data is not transmitted to GA. You can either
* copy & paste the script there, or include it as a file with the PHP
* function include() or the WordPress function get_template_part().
*
* As an alternative, you could place this script wherever you want and
* transmit the data to GA using a non-interaction event. If you do so,
* you'll see the content grouping data in the Event -> Pages report
* rather than in the Content -> All Pages one.
*
* This script was improved thanks to the following resources; thanks
* to the authors!
* - https://www.highposition.com/blog/how-to-send-author-content-groups-
* wordpress-google-analytics/
* - http://stackoverflow.com/a/34714363/2972183
*
* Created by Guido W. Pettinari on 08.01.2015.
* Part of WordPress Analytics:
* https://github.com/coccoinomane/wordpress_analytics
*/
function wpan_content_grouping() {
/* Extract the content grouping options from the database. If these are
not properly formatted, nothing will be sent to GA. */
$options = wpan_get_options ();
$ga_tracker = isset ( $options ['tracker_name'] ) ? $options ['tracker_name'] : '';
$wordpress_group = isset ( $options['group_index_wordpress'] ) ? $options['group_index_wordpress'] : '';
$woocommerce_group = isset ( $options['group_index_woocommerce'] ) ? $options['group_index_woocommerce'] : '';
$blog_group = isset ( $options['group_index_blog'] ) ? $options['group_index_blog'] : '';
/* Extract the categories of this post, and select from them the
top-level category that is first in alphabetical order. Note that
in WordPress it is possible for posts not to have top-level
categories; in that case, the category name will be empty and
the post will be catalogued as (not set) in GA. */
$categories = get_the_category();
foreach ($categories as $cat) {
if ($cat->category_parent == 0) {
$category_name = $cat->cat_name;
break;
}
}
// ====================================================================================
// = Product page =
// ====================================================================================
if (function_exists('is_product') && is_product()) {
echo "<script> $ga_tracker('set', 'contentGroup" . $wordpress_group . "', '" . "Prodotti" . "'); </script>\n";
/* Extract the terms in the product category attached this post, and select from
them the top-level term that is first in alphabetical order. Note that in WordPress
it is possible for posts not to have top-level terms; in that case, the category
name will be empty and the post will be catalogued as (not set) in GA. */
$terms = get_the_terms (get_the_ID(), 'product_cat');
foreach ($terms as $term) {
if ($term->parent == 0) {
$term_name = $term->name;
break;
}
}
if ( $terms && !is_wp_error($terms) && !empty($term_name))
echo "<script> $ga_tracker('set', 'contentGroup" . $woocommerce_group . "', '" . $term_name . "'); </script>\n";
else
echo "<script> $ga_tracker('set', 'contentGroup" . $woocommerce_group . "', '" . "Undefined Product" . "'); </script>\n";
}
// ====================================================================================
// = Content page =
// ====================================================================================
/* Default behaviour: send to Analytics the post category from WordPress, and
the information stored in the custom field 'analytics_content_type' */
else if ( $categories && ! is_wp_error($categories) && ! empty($category_name) ) {
echo "<script> $ga_tracker('set', 'contentGroup" . $wordpress_group . "', '" . $category_name . "'); </script>\n";
/* Extract content type from custom field */
$content_type = get_post_meta(get_the_ID(), 'analytics_content_type', true);
if ($content_type)
echo "<script> $ga_tracker('set', 'contentGroup" . $blog_group . "', '" . $content_type . "'); </script>\n";
}
} // wpan_content_grouping
/* If Advanced Custom Fields is installed, load all JSON files
in the acf subdirectory */
if ( class_exists( 'acf' ) && isset ( $options['enable_json_folder'] ) && $options['enable_json_folder'] ) {
add_filter( 'acf/settings/load_json', 'my_acf_json_load_point' );
function my_acf_json_load_point( $paths ) {
$paths[] = WPAN_PLUGIN_DIR . "acf";
return $paths;
}
} // if acf
?>