Title: [Plugin: Google Analytics for WordPress] Load jQuery and comment tracking code modification request
Last modified: August 20, 2016

---

# [Plugin: Google Analytics for WordPress] Load jQuery and comment tracking code modification request

 *  [Todd Lahman](https://wordpress.org/support/users/toddlahman/)
 * (@toddlahman)
 * [14 years, 2 months ago](https://wordpress.org/support/topic/plugin-google-analytics-for-wordpress-load-jquery-and-comment-tracking-code-modification-request/)
 * On pages that have no comments there is no need to load jQuery or the comment-
   reply script. To make pages load faster I modified your plugin to only load the
   comment tracking code and jQuery when there are more than zero comments. I have
   provided the code below, and was hoping you could add this to the plugin.
 * In the googleanalytics.php file from line 1449 to line 1490 (after the code modification)
   below:
 *     ```
       function track_comment_form_head() {
           if (is_singular() && get_comments_number() > 0) {
               global $post;
               $yoast_ga_options = get_option('Yoast_Google_Analytics');
               if ( yoast_ga_do_tracking()
       			&& isset( $yoast_ga_options["trackcommentform"] )
       			&& $yoast_ga_options["trackcommentform"]
       			&& ( 'open' == $post->comment_status ) )
                   wp_enqueue_script('jquery');
           }
       }
       add_action('wp_print_scripts','track_comment_form_head');
   
       $comment_form_id = 'commentform';
       function yoast_get_comment_form_id($args) {
       	global $comment_form_id;
       	$comment_form_id = $args['id_form'];
       	return $args;
       }
       add_filter('comment_form_defaults', 'yoast_get_comment_form_id',99,1);
   
       function yoast_track_comment_form() {
       	if (get_comments_number() > 0) {
           global $comment_form_id, $post;
           $yoast_ga_options = get_option('Yoast_Google_Analytics');
           if ( yoast_ga_do_tracking() && $yoast_ga_options["trackcommentform"] ) {
       ?>
       <script type="text/javascript">
           jQuery(document).ready(function() {
               jQuery('#<?php echo $comment_form_id; ?>').submit(function() {
                   _gaq.push(
                       ['_setAccount','<?php echo $yoast_ga_options["uastring"]; ?>'],
                       ['_trackEvent','comment','submit']
                   );
               });
           });
       </script>
       <?php
           }
         }
       }
       add_action('comment_form_after','yoast_track_comment_form');
       ```
   
 * [http://wordpress.org/extend/plugins/google-analytics-for-wordpress/](http://wordpress.org/extend/plugins/google-analytics-for-wordpress/)

The topic ‘[Plugin: Google Analytics for WordPress] Load jQuery and comment tracking
code modification request’ is closed to new replies.

 * ![](https://ps.w.org/google-analytics-for-wordpress/assets/icon.svg?rev=2976619)
 * [MonsterInsights - Google Analytics Dashboard for WordPress (Website Stats Made Easy)](https://wordpress.org/plugins/google-analytics-for-wordpress/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/google-analytics-for-wordpress/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/google-analytics-for-wordpress/)
 * [Active Topics](https://wordpress.org/support/plugin/google-analytics-for-wordpress/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/google-analytics-for-wordpress/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/google-analytics-for-wordpress/reviews/)

 * 0 replies
 * 1 participant
 * Last reply from: [Todd Lahman](https://wordpress.org/support/users/toddlahman/)
 * Last activity: [14 years, 2 months ago](https://wordpress.org/support/topic/plugin-google-analytics-for-wordpress-load-jquery-and-comment-tracking-code-modification-request/)
 * Status: not resolved