• Resolved Charles

    (@charlesrodmell)


    I’m trying to figure out how I’d trigger a GA4 event from Google Tag Manager when anyone posts a comment on a post. I tried click text = “Post Comment”, but Tag Manager doesn’t see any click text. Additionally, people could click it without succeeding in submitting a comment. Any ideas?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    In DOM parlance, the button text is not a literal “text” property but a “value” attribute. Selecting elements by ID should be more reliable than other methods. The correct ID is somewhat theme dependent, but many themes use the comment_form() function, where the post comment button’s ID is “submit”.

    As you point out, this simply tracks a click event. There’s no indication that the submission would be successful or not. Tracking a successful comment submission is more complicated. You’d need to track the page load that contains the new comment. This would mean using PHP to distinguish if the page contains a new comment from the current user and to have PHP then output the necessary script block that will cause the page load to be tracked. Untested, but this might be done by hooking the “comment_post” action which fires when any new comment is added to the DB. An array of comment data is passed in case that’s of any use.

    The callback for this action could either enqueue the necessary script or add another callback to “wp_print_scripts” action to conditionally output the script block directly. Be advised that using “wp_print_scripts” is frowned upon, it’s intended for internal use only. However it remains usable all the same. However, doing so makes resolving dependencies difficult and why enqueuing is preferred.

    Thread Starter Charles

    (@charlesrodmell)

    Cheers. I tried this snippet based on your action hook idea, but I don’t see it in the datalayer when commenting, so I must have messed something up:

    function custom_comment_post_event() {
            $data_layer = array(
                'event' => 'comment_posted'
             );
    
            echo '<script>dataLayer.push(' . json_encode( $data_layer ) . ');</script>';
    }
    add_action( 'comment_post', 'custom_comment_post_event' );
    Moderator bcworkz

    (@bcworkz)

    I don’t think “comment_post” action is the right place to echo out script. “wp_print_scripts” would be the proper action. But “wp_print_scripts” has no context about what comment had been posted, if any. What you want to do is hook “wp_print_scripts” from within your “comment_post” callback. This will cause the script to only be output when a comment had been posted.

    In your add_action() call, you may want to pass a proper $priority value (3rd parameter) to help ensure the script is output before or after other scripts it may be dependent on. Unless you can find out how dependent scripts are enqueued, the proper value might need to be determined by trial and error.

    It might be OK to unconditionally hook “wp_print_scripts” from “comment_post” callback for now, but you will eventually want to check the passed information to determine if the comment had actually been approved. You don’t want to trigger GA tracking events for unapproved or spam comments.

    Thread Starter Charles

    (@charlesrodmell)

    Oh gah, yeah. Forgot about approval. Cheers. Keep up the good work ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Analytics tracking for comments’ is closed to new replies.