-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchartbeat.php
107 lines (87 loc) · 3.98 KB
/
chartbeat.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
<?php
function chartbeat_configs() {
$domain = 'bandab.com.br';
$user_id = 66994;
$cb_configs['domain'] = $domain;
$cb_configs['uid'] = $user_id;
if (is_single()) {
$post = get_queried_object();
/* só verificar essa parte, porque sei que existe um campo personalizado para puxar o nome do autor no WP */
$author = get_the_author_meta('display_name', $post->post_author);
$cb_configs['author'] = apply_filters( 'chartbeat_config_author', $author );
$cats = get_the_terms($post->ID, 'category');
if ($cats) {
$cat_names = array();
foreach ( $cats as $cat ) {
$cat_names[] = $cat->name;
}
}
$sections = (array)apply_filters( 'chartbeat_config_sections', $cat_names );
$cb_configs['sections'] = implode( ", ", $sections);
}
return $cb_configs;
}
function add_chartbeat_head() {
$cb_configs = chartbeat_configs();
$cb_headline_testing = true;
?>
<script data-cfasync="false" type="text/javascript">
var _sf_async_config = window._sf_async_config = (window._sf_async_config || {});
_sf_async_config.uid = <?php echo esc_js($cb_configs["uid"]); ?>;
_sf_async_config.domain = "<?php echo esc_js($cb_configs["domain"]); ?>";
_sf_async_config.useCanonical = true;
_sf_async_config.useCanonicalDomain = true;
<?php if (is_single()) {?>
_sf_async_config.authors = "<?php echo esc_js($cb_configs["author"]); ?>";
_sf_async_config.sections = "<?php echo esc_js($cb_configs["sections"]); ?>";
<?php } ?>
(function() {
function loadChartbeat() {var e = document.createElement('script');var n = document.getElementsByTagName('script')[0];e.type = 'text/javascript';e.setAttribute("data-cfasync", "false");e.async = true;e.src = '//static.chartbeat.com/js/chartbeat.js';n.parentNode.insertBefore(e, n);} loadChartbeat();
<?php if ($cb_headline_testing == 1) {?>
function loadChartbeatMAB() {var e = document.createElement('script');var n = document.getElementsByTagName('script')[0];e.type = 'text/javascript';e.setAttribute("data-cfasync", "false");e.async = true;e.src = '//static.chartbeat.com/js/chartbeat_mab.js';n.parentNode.insertBefore(e, n);} loadChartbeatMAB();
<?php } ?>
})();
</script>
<?php
}
add_action('wp_head', 'add_chartbeat_head');
function chartbeat_amp_add_analytics( $analytics ) {
if ( ! is_array( $analytics ) ) {
$analytics = array();
}
$cb_configs = chartbeat_configs();
$analytics['chartbeat'] = array(
'type' => 'chartbeat',
'attributes' => array(),
'config_data' => array(
'vars' => array(
'uid' => esc_js($cb_configs["uid"]),
'domain' => esc_js($cb_configs["domain"]),
)
),
);
if (is_single()) {
$analytics['chartbeat']['config_data']['vars']['authors'] = esc_js($cb_configs['author']);
$analytics['chartbeat']['config_data']['vars']['sections'] = esc_js($cb_configs['sections']);
}
return $analytics;
}
add_filter( 'amp_post_template_analytics', 'chartbeat_amp_add_analytics' );
use Facebook\InstantArticles\Elements\Analytics;
add_action( 'instant_articles_after_transform_post', function ($ia_post) {
$instant_article = $ia_post->instant_article;
$cb_configs = chartbeat_configs();
$cbia_start = '<script type="text/javascript">
(function() {
var _sf_async_config = window._sf_async_config = (window._sf_async_config || {});
_sf_async_config.path = window.ia_document.shareURL;
_sf_async_config.title = window.ia_document.title; ';
$cbia_config = '_sf_async_config.uid = "' . $cb_configs["uid"] . '"; _sf_async_config.domain = "' . $cb_configs["domain"] . '"; ';
if (is_single()) {
$cbia_config .= '_sf_async_config.authors = "' . $cb_configs["author"] . '";_sf_async_config.sections = "' . $cb_configs["sections"] . '";';
}
$cbia_end = '})();</script><script src="//static.chartbeat.com/js/chartbeat_fia.js"></script>';
$cbia_script = $cbia_start . $cbia_config . $cbia_end;
$instant_article->addChild(Analytics::create()->withHTML($cbia_script));
});
?>