22/*
33Plugin Name: Fathom Analytics
44Description: A simple plugin to add the Fathom tracking snippet to your WordPress site.
5- Author: Fathom Team
6- Version: 1.0.1
5+ Author: Conva Ventures Inc
6+ Note: Huge thanks to Danny Van Kooten for his hard work on Version 1 of this plugin
7+ Version: 2.0.0
78
89Fathom Analytics for WordPress
9- Copyright (C) 2018 Danny van Kooten
10+ Copyright (C) 2019 Conva Ventures Inc
1011
1112This program is free software: you can redistribute it and/or modify
1213it under the terms of the GNU General Public License as published by
2425
2526const FATHOM_URL_OPTION_NAME = 'fathom_url ' ;
2627const FATHOM_SITE_ID_OPTION_NAME = 'fathom_site_id ' ;
28+ const FATHOM_PRIVATE_SHARE_PASSWORD = 'fathom_share_password ' ;
29+ const FATHOM_SHOW_ANALYTICS_MENU_ITEM = 'fathom_show_menu ' ;
2730
2831/**
2932* @since 1.0.0
3033*/
3134function fathom_get_url () {
3235 $ fathom_url = get_option ( FATHOM_URL_OPTION_NAME , '' );
33-
36+
3437 // don't print snippet if fathom URL is empty
3538 if ( empty ( $ fathom_url ) ) {
36- return '' ;
39+ return 'cdn.usefathom.com ' ;
3740 }
3841
3942 // trim trailing slash
@@ -47,7 +50,7 @@ function fathom_get_url() {
4750
4851/**
4952* @since 1.0.1
50- */
53+ */
5154function fathom_get_site_id () {
5255 return get_option ( FATHOM_SITE_ID_OPTION_NAME , '' );
5356}
@@ -57,14 +60,13 @@ function fathom_get_site_id() {
5760*/
5861function fathom_print_js_snippet () {
5962 $ url = fathom_get_url ();
60-
61- // don't print snippet if fathom URL is empty
62- if ( empty ( $ url ) ) {
63- return ;
64- }
6563
6664 $ site_id = fathom_get_site_id ();
6765
66+ if (empty ($ site_id )) {
67+ return ;
68+ }
69+
6870 ?>
6971 <!-- Fathom - simple website analytics - https://usefathom.com/ -->
7072 <script>
@@ -77,32 +79,56 @@ function fathom_print_js_snippet() {
7779 m=f.getElementsByTagName('script')[0];
7880 o.async=1; o.src=t;
7981 m.parentNode.insertBefore(o,m)
80- })(document, window, '//<?php echo esc_attr ( $ url ); ?> /tracker.js', 'fathom');
81- fathom('set', 'siteId', '<?php echo esc_attr ( $ site_id ); ?> ');
82+ })(document, window, 'https: //<?php echo esc_attr ( $ url ); ?> /tracker.js', 'fathom');
83+ fathom('set', 'siteId', '<?php echo esc_attr ( $ site_id ); ?> ');
8284 fathom('trackPageview');
8385 </script>
8486 <!-- / Fathom -->
8587 <?php
8688}
8789
90+ /**
91+ * @since 2.0.0
92+ */
93+ function fathom_stats_page () {
94+ add_menu_page ( 'Fathom ' , 'Analytics ' , 'edit_pages ' , 'analytics ' , 'fathom_print_stats_page ' , 'dashicons-chart-bar ' , 6 );
95+ }
96+
97+ /**
98+ * @since 2.0.0
99+ */
100+ function fathom_print_stats_page () {
101+ wp_enqueue_script ('fathom-iframresize ' , plugins_url ('iframeResizer.min.js ' , __FILE__ ));
102+ wp_enqueue_script ('fathom-stats-iframe ' , plugins_url ('fathom-stats-iframe.js ' , __FILE__ ));
103+ echo '<div class="wrap"> ' ;
104+ echo '<iframe id="fathom-stats-iframe" src="https://app.usefathom.com/share/ ' . get_option ( FATHOM_SITE_ID_OPTION_NAME ) . '/wordpress?password= ' . hash ('sha256 ' , get_option ( FATHOM_PRIVATE_SHARE_PASSWORD )) . '" style="width: 1px;min-width: 100%; height:1000px; max-width:1100px" frameborder="0" onload=fathomResizeIframe();></iframe> ' ;
105+ echo '</div> ' ;
106+ }
107+
88108/**
89109* @since 1.0.0
90110*/
91111function fathom_register_settings () {
92- $ fathom_logo_html = sprintf ( '<a href="https://usefathom.com/" style="margin-left: 6px;"><img src="%s" width=16 height=16 style="vertical-align: bottom;"></a> ' , plugins_url ( 'fathom.svg ' , __FILE__ ) );
93-
112+ $ fathom_logo_html = sprintf ( '<a href="https://usefathom.com/" style="margin-left: 6px;"><img src="%s" width=20 height=20 style="vertical-align: bottom;"></a> ' , plugins_url ( 'fathom.png ' , __FILE__ ) );
113+
94114 // register page + section
95115 add_options_page ( 'Fathom Analytics ' , 'Fathom Analytics ' , 'manage_options ' , 'fathom-analytics ' , 'fathom_print_settings_page ' );
96116 add_settings_section ( 'default ' , "Fathom Analytics {$ fathom_logo_html }" , '__return_true ' , 'fathom-analytics ' );
97117
98118 // register options
99- register_setting ( 'fathom ' , FATHOM_URL_OPTION_NAME , array ( 'type ' => 'string ' ) );
100119 register_setting ( 'fathom ' , FATHOM_SITE_ID_OPTION_NAME , array ( 'type ' => 'string ' ) );
120+ register_setting ( 'fathom ' , FATHOM_URL_OPTION_NAME , array ( 'type ' => 'string ' ) );
101121
102- // register settings fields
103- add_settings_field ( FATHOM_URL_OPTION_NAME , __ ( ' Dashboard URL ' , ' fathom-analytics ' ), ' fathom_print_url_setting_field ' , ' fathom-analytics ' , ' default ' );
122+ register_setting ( ' fathom ' , FATHOM_PRIVATE_SHARE_PASSWORD , array ( ' type ' => ' string ' ) );
123+ register_setting ( ' fathom ' , FATHOM_SHOW_ANALYTICS_MENU_ITEM , array ( ' type ' => ' boolean ' ) );
104124
125+ // register settings fields
105126 add_settings_field ( FATHOM_SITE_ID_OPTION_NAME , __ ( 'Site ID ' , 'fathom-analytics ' ), 'fathom_print_site_id_setting_field ' , 'fathom-analytics ' , 'default ' );
127+ add_settings_field ( FATHOM_SHOW_ANALYTICS_MENU_ITEM , __ ( 'Display Analytics Menu Item ' , 'fathom-analytics ' ), 'fathom_print_display_analytics_menu_setting_field ' , 'fathom-analytics ' , 'default ' );
128+ add_settings_field ( FATHOM_PRIVATE_SHARE_PASSWORD , __ ( 'Fathom Share Password ' , 'fathom-analytics ' ), 'fathom_print_share_password_setting_field ' , 'fathom-analytics ' , 'default ' );
129+
130+ add_settings_field ( FATHOM_URL_OPTION_NAME , __ ( 'Fathom URL ' , 'fathom-analytics ' ), 'fathom_print_url_setting_field ' , 'fathom-analytics ' , 'default ' );
131+
106132}
107133
108134/**
@@ -118,14 +144,32 @@ function fathom_print_settings_page() {
118144 echo '</div> ' ;
119145}
120146
147+ /**
148+ * @since 2.0.0
149+ */
150+ function fathom_print_display_analytics_menu_setting_field ( $ args = array () ) {
151+ $ value = get_option ( FATHOM_SHOW_ANALYTICS_MENU_ITEM );
152+ echo sprintf ( '<input type="checkbox" name="%s" id="%s" class="regular-text" ' . (esc_attr ($ value ) ? 'checked ' : '' ) .' /> ' , FATHOM_SHOW_ANALYTICS_MENU_ITEM , FATHOM_SHOW_ANALYTICS_MENU_ITEM );
153+ echo '<p class="description"> ' . __ ( 'Display the Fathom Tab ' , 'fathom-analytics ' ) . '</p> ' ;
154+ }
155+
156+ /**
157+ * @since 2.0.0
158+ */
159+ function fathom_print_share_password_setting_field ( $ args = array () ) {
160+ $ value = get_option ( FATHOM_PRIVATE_SHARE_PASSWORD );
161+ echo sprintf ( '<input type="text" name="%s" id="%s" class="regular-text" value="%s" placeholder="%s" /> ' , FATHOM_PRIVATE_SHARE_PASSWORD , FATHOM_PRIVATE_SHARE_PASSWORD , esc_attr ( $ value ), esc_attr ( $ placeholder ) );
162+ echo '<p class="description"> ' . __ ( 'If you don \'t set a password here, the user will be prompted to enter one on the Analytics menu option ' , 'fathom-analytics ' ) . '</p> ' ;
163+ }
164+
121165/**
122166* @since 1.0.0
123167*/
124168function fathom_print_url_setting_field ( $ args = array () ) {
125169 $ value = get_option ( FATHOM_URL_OPTION_NAME );
126170 $ placeholder = 'https://my-stats.usefathom.com/ ' ;
127- echo sprintf ( '<input type="text" name="%s" id="%s" class="regular-text" value="%s" placeholder="%s" /> ' , FATHOM_URL_OPTION_NAME , FATHOM_URL_OPTION_NAME , esc_attr ( $ value ), esc_attr ( $ placeholder ) );
128- echo '<p class="description"> ' . __ ( 'Enter the full URL to your Fathom instance here. ' , 'fathom-analytics ' ) . '</p> ' ;
171+ echo sprintf ( '<input type="text" name="%s" id="%s" class="regular-text" value="%s" placeholder="%s" /> ' , FATHOM_URL_OPTION_NAME , FATHOM_URL_OPTION_NAME , esc_attr ( $ value ) ?: ' cdn.usefathom.com ' , esc_attr ( $ placeholder ) );
172+ echo '<p class="description"> ' . __ ( 'Only edit this value if you are using Fathom Lite ' , 'fathom-analytics ' ) . '</p> ' ;
129173}
130174
131175/**
@@ -135,11 +179,15 @@ function fathom_print_site_id_setting_field( $args = array() ) {
135179 $ value = get_option ( FATHOM_SITE_ID_OPTION_NAME );
136180 $ placeholder = 'ABCDEF ' ;
137181 echo sprintf ( '<input type="text" name="%s" id="%s" class="regular-text" value="%s" placeholder="%s" /> ' , FATHOM_SITE_ID_OPTION_NAME , FATHOM_SITE_ID_OPTION_NAME , esc_attr ( $ value ), esc_attr ( $ placeholder ) );
138- echo '<p class="description"> ' . __ ( 'Find your site ID by by clicking the gearwheel in your Fathom dashboard. ' , 'fathom-analytics ' ) . '</p> ' ;
182+ echo '<p class="description"> ' . __ ( 'This is the <a href="https://usefathom.com/support/wordpress" target="_blank">unique Tracking ID</a> for your site ' , 'fathom-analytics ' ) . '</p> ' ;
139183}
140184
141185add_action ( 'wp_head ' , 'fathom_print_js_snippet ' , 50 );
142186
143187if ( is_admin () && ! wp_doing_ajax () ) {
144188 add_action ( 'admin_menu ' , 'fathom_register_settings ' );
145189}
190+
191+ if (get_option ( FATHOM_SHOW_ANALYTICS_MENU_ITEM )) {
192+ add_action ( 'admin_menu ' , 'fathom_stats_page ' );
193+ }
0 commit comments