- Navigate to the ch2-page-header-output folder of the WordPress plugin directory of your development installation.
- Open the ch2-page-header-output.php file in a text editor.
- Modify the implementation of the ch2pho_page_header_output function to retrieve the plugin options array and use the stored value for the account number to embed it in the page header code. The new sections are identified in bold:
function ch2pho_page_header_output() {
$options = ch2pho_get_options();
?>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;
i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();
a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;
m.parentNode.insertBefore(a,m)})(window,document,'script',
'https://www.google-analytics.com/analytics.js','ga');
ga( 'create', '<?php echo $options['ga_account_name']; ?>',
'auto' );
ga('send', 'pageview');
</script>
<?php }
- Add code to check whether outgoing code tracking should be done before registering an action hook to filter all post and page content, with the changes made identified in bold:
$options = ch2pho_get_options();
if ( true == $options['track_outgoing_links'] ) {
add_filter( 'the_content','ch2lfa_link_filter_analytics' );
}
- Use the same check to determine whether page footer code should be added to provide the JavaScript necessary for outgoing link tracking to occur with the changes made identified in bold:
if ( true == $options['track_outgoing_links'] ) {
add_action( 'wp_footer', 'ch2lfa_footer_analytics_code' );
}
- Save and close the plugin file.
- Visit the site and look at the page source to see that the previous UA-0000000-0 has been replaced by the last value saved in the plugin's configuration page. You can also set the link tracking code to be displayed or not by changing the track outgoing links option.