WordPress
Two ways to install: a small real plugin, or paste the snippet into your theme.
Option 1: Plugin
Create a file named shonylabs-analytics.php in wp-content/plugins/shonylabs-analytics/ with the contents below, then activate it from Plugins in wp-admin and enter your Site ID under Settings → ShonyLabs Analytics.
PHP
<?php
/**
* Plugin Name: ShonyLabs Analytics
* Description: Adds the ShonyLabs Analytics tracking snippet to every page.
* Version: 1.0.0
*/
if (!defined('ABSPATH')) {
exit;
}
function shonylabs_analytics_head() {
$site_id = get_option('shonylabs_site_id', '');
if (empty($site_id)) {
return;
}
printf(
'<script defer data-site="%1$s" src="https://api.shonylabs.com/js/%1$s.js"></script>' . "\n",
esc_attr($site_id)
);
}
add_action('wp_head', 'shonylabs_analytics_head');
function shonylabs_analytics_settings_page() {
add_options_page('ShonyLabs Analytics', 'ShonyLabs Analytics', 'manage_options', 'shonylabs-analytics', 'shonylabs_analytics_render_settings');
}
add_action('admin_menu', 'shonylabs_analytics_settings_page');
function shonylabs_analytics_register_settings() {
register_setting('shonylabs_analytics', 'shonylabs_site_id');
}
add_action('admin_init', 'shonylabs_analytics_register_settings');
function shonylabs_analytics_render_settings() {
?>
<div class="wrap">
<h1>ShonyLabs Analytics</h1>
<form method="post" action="options.php">
<?php settings_fields('shonylabs_analytics'); ?>
<table class="form-table">
<tr>
<th scope="row"><label for="shonylabs_site_id">Site ID</label></th>
<td><input type="text" id="shonylabs_site_id" name="shonylabs_site_id" value="<?php echo esc_attr(get_option('shonylabs_site_id', '')); ?>" class="regular-text" placeholder="YOUR_SITE_ID" /></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}Option 2: Paste the snippet directly
If you'd rather not add a plugin, paste this into your theme's header.php right before </head> (Appearance → Theme File Editor), or into any "Insert Headers and Footers" style plugin you already use.
HTML
<script defer data-site="YOUR_SITE_ID" src="https://api.shonylabs.com/js/YOUR_SITE_ID.js"></script>A child theme (or the plugin above) survives theme updates; editing the active theme's
header.php directly does not.