2222along with this program. If not, see <https://www.gnu.org/licenses/>.
2323*/
2424
25- const FATHOM_OPTION_NAME = 'fathom_url ' ;
26-
27- function fathom_print_js_snippet () {
28- $ fathom_url = get_option ( FATHOM_OPTION_NAME , '' );
25+ const FATHOM_URL_OPTION_NAME = 'fathom_url ' ;
26+ const FATHOM_SITE_ID_OPTION_NAME = 'fathom_site_id ' ;
2927
28+ function fathom_get_url () {
29+ $ fathom_url = get_option ( FATHOM_URL_OPTION_NAME , '' );
30+
3031 // don't print snippet if fathom URL is empty
3132 if ( empty ( $ fathom_url ) ) {
32- return ;
33+ return '' ;
3334 }
3435
3536 // trim trailing slash
@@ -38,7 +39,24 @@ function fathom_print_js_snippet() {
3839 // make relative
3940 $ fathom_url = str_replace ( array ( 'https: ' , 'http: ' ), '' , $ fathom_url );
4041
41- ?>
42+ return $ fathom_url ;
43+ }
44+
45+ function fathom_get_site_id () {
46+ return get_option ( FATHOM_SITE_ID_OPTION_NAME , '' );
47+ }
48+
49+ function fathom_print_js_snippet () {
50+ $ url = fathom_get_url ();
51+
52+ // don't print snippet if fathom URL is empty
53+ if ( empty ( $ url ) ) {
54+ return ;
55+ }
56+
57+ $ site_id = fathom_get_site_id ();
58+
59+ ?>
4260 <!-- Fathom - simple website analytics - https://github.com/usefathom/fathom -->
4361 <script>
4462 (function(f, a, t, h, o, m){
@@ -50,7 +68,8 @@ function fathom_print_js_snippet() {
5068 m=f.getElementsByTagName('script')[0];
5169 o.async=1; o.src=t;
5270 m.parentNode.insertBefore(o,m)
53- })(document, window, '//<?php echo esc_attr ( $ fathom_url ); ?> /tracker.js', 'fathom');
71+ })(document, window, '//<?php echo esc_attr ( $ url ); ?> /tracker.js', 'fathom');
72+ fathom('set', 'siteId', '<?php echo esc_attr ( $ site_id ); ?> ');
5473 fathom('trackPageview');
5574 </script>
5675 <!-- / Fathom -->
@@ -61,20 +80,31 @@ function fathom_print_js_snippet() {
6180
6281function fathom_register_settings () {
6382 // register option
64- register_setting ( 'general ' , FATHOM_OPTION_NAME , array ( 'type ' => 'string ' ) );
83+ register_setting ( 'general ' , FATHOM_URL_OPTION_NAME , array ( 'type ' => 'string ' ) );
84+ register_setting ( 'general ' , FATHOM_SITE_ID_OPTION_NAME , array ( 'type ' => 'string ' ) );
6585
66- // register settings field
86+ // register settings fields
6787 $ title = __ ( 'Fathom URL ' , 'fathom-analytics ' );
68- add_settings_field ( FATHOM_OPTION_NAME , $ title , 'fathom_print_setting_field ' , 'general ' );
88+ add_settings_field ( FATHOM_URL_OPTION_NAME , $ title , 'fathom_print_url_setting_field ' , 'general ' );
89+
90+ $ title = __ ( 'Fathom Site ID ' , 'fathom-analytics ' );
91+ add_settings_field ( FATHOM_SITE_ID_OPTION_NAME , $ title , 'fathom_print_site_id_setting_field ' , 'general ' );
6992}
7093
71- function fathom_print_setting_field ( $ args = array () ) {
72- $ value = get_option ( FATHOM_OPTION_NAME );
94+ function fathom_print_url_setting_field ( $ args = array () ) {
95+ $ value = get_option ( FATHOM_URL_OPTION_NAME );
7396 $ placeholder = 'http://my-stats.usefathom.com/ ' ;
74- echo sprintf ( '<input type="text" name="%s" id="%s" class="regular-text" value="%s" placeholder="%s" /> ' , FATHOM_OPTION_NAME , FATHOM_OPTION_NAME , esc_attr ( $ value ), esc_attr ( $ placeholder ) );
97+ 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 ) );
7598 echo '<p class="description"> ' . __ ( 'Enter the full URL to your Fathom instance here. ' , 'fathom-analytics ' ) . '</p> ' ;
7699}
77100
101+ function fathom_print_site_id_setting_field ( $ args = array () ) {
102+ $ value = get_option ( FATHOM_SITE_ID_OPTION_NAME );
103+ $ placeholder = 'xx-xx ' ;
104+ 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 ) );
105+ echo '<p class="description"> ' . __ ( 'Enter your Fathom site ID here ' , 'fathom-analytics ' ) . '</p> ' ;
106+ }
107+
78108if ( is_admin () && ! wp_doing_ajax () ) {
79109 add_action ( 'admin_menu ' , 'fathom_register_settings ' );
80110}
0 commit comments