-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ampanalyticssupport.php
131 lines (103 loc) · 4.57 KB
/
ampanalyticssupport.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
<?php
/*
* Plugin Name: AMP Google Analytics 4 Support
* Plugin URI: https://github.com/rolandfarkasCOM/ampanalyticssupport/
* Description: Google Analytics 4 (GA4) AMP support plugin.
* Version: 1.1.6
* Author: Roland Farkas
* Author URI: https://rolandfarkas.com
* License GPLv3 or later
* Text Domain: ampanalyticssupport
* Requires at least: 4.9
* Requires PHP: 5.6
*
* @package ampanalyticssupport
*/
// If this file is called directly, abort!!!
defined( 'ABSPATH' ) or die( 'Hey, what are you doing here? You silly human!' );
if ( !class_exists( 'ampanalyticssupportPlugin' ) ) {
class ampanalyticssupportPlugin
{
public $plugin;
function __construct() {
$this->plugin = plugin_basename( __FILE__ );
}
function register() {
add_filter( "plugin_action_links_$this->plugin", array( $this, 'settings_link' ) );
add_action( 'admin_menu', array( $this, 'add_admin_pages' ) );
}
public function settings_link( $links ) {
$settings_link = '<a href="admin.php?page=ampanalyticssupport">Settings</a>';
array_push( $links, $settings_link );
return $links;
}
public function add_admin_pages() {
$page_title = 'AMP GA4 Support';
$menu_title = 'AMP GA4 Support';
$capability = 'manage_options';
$menu_slug = 'ampanalyticssupport';
$function = array( $this, 'admin_index' );
$icon_url = 'dashicons-analytics';
$position = 100;
add_menu_page( $page_title,
$menu_title,
$capability,
$menu_slug,
$function,
$icon_url,
$position );
}
public function admin_index() {
require_once plugin_dir_path( __FILE__ ) . 'templates/admin.php';
}
function activate() {
require_once plugin_dir_path( __FILE__ ) . 'inc/ampanalyticssupport-plugin-activate.php';
$ampanalyticssupportActivate = new ampanalyticssupportActivate();
$ampanalyticssupportActivate->activate();
}
}
$ampanalyticssupportPlugin = new ampanalyticssupportPlugin();
$ampanalyticssupportPlugin->register();
// activation
register_activation_hook( __FILE__, array( $ampanalyticssupportPlugin, 'activate' ) );
register_setting( 'ampanalyticssupport-settings', 'ampanalyticssupport' );
register_setting( 'ampanalyticssupport-settings', 'ampanalyticssupport_pageview' );
register_setting( 'ampanalyticssupport-settings', 'ampanalyticssupport_consent' );
register_setting( 'ampanalyticssupport-settings', 'ampanalyticssupport_webvitals' );
register_setting( 'ampanalyticssupport-settings', 'ampanalyticssupport_performance' );
}
function page_is_amp() {
if ( function_exists( 'amp_is_request' ) ):
return amp_is_request();
else :
return false;
endif;
}
// Check if the request is amp and if the amp plugin is active and insert analytics if required.
function ampanalyticssupport() {
include_once ABSPATH . 'wp-admin/includes/plugin.php';
if ( is_plugin_active( 'amp/amp.php' ) && page_is_amp()) {
//plugin is activated
$ampanalyticssupport = '<amp-analytics type="googleanalytics" config="'.plugin_dir_url( __FILE__ ).'ga4.json" data-credentials="include"><script type="application/json">{"vars": {';
// Adding backwards compatibility to AMP Google Analytics 4 Support v1.1.0
if(get_option('ampanalyticssupport_pageview') != "" && get_option('ampanalyticssupport_consent') != "" && get_option('ampanalyticssupport_webvitals') != "" && get_option('ampanalyticssupport_performance') != ""){
$ampanalyticssupport .= '"GA4_MEASUREMENT_ID": "'.get_option('ampanalyticssupport').'",
"GA4_ENDPOINT_HOSTNAME": "www.google-analytics.com",
"DEFAULT_PAGEVIEW_ENABLED": '.get_option('ampanalyticssupport_pageview').',
"GOOGLE_CONSENT_ENABLED": '.get_option('ampanalyticssupport_consent').',
"WEBVITALS_TRACKING": '.get_option('ampanalyticssupport_webvitals').',
"PERFORMANCE_TIMING_TRACKING": '.get_option('ampanalyticssupport_performance').'}}</script></amp-analytics>';
}else{
$ampanalyticssupport .= '"GA4_MEASUREMENT_ID": "'.get_option('ampanalyticssupport').'",
"GA4_ENDPOINT_HOSTNAME": "www.google-analytics.com",
"DEFAULT_PAGEVIEW_ENABLED": true,
"GOOGLE_CONSENT_ENABLED": false,
"WEBVITALS_TRACKING": false,
"PERFORMANCE_TIMING_TRACKING": false}}</script></amp-analytics>';
}
echo $ampanalyticssupport;
}
}
add_action('wp_footer', 'ampanalyticssupport');
add_action('amp_post_template_footer', 'ampanalyticssupport');
?>