I am trying to use the comment_post action to pass data for this action to Intercom which I am using to track users’ behaviour/interaction.

The function seems to be working fine aside from the JS part of the function as Intercom is not received anything.

add_action('comment_post', 'siv_analytics_comment_event', 10, 2);   
function siv_analytics_comment_event( $comment_ID ){    
    if( 1 === $comment_approved ){
            $comment_data = get_comment($comment_ID);
            $comment_post_id = $comment_data->comment_post_ID;

            $post = get_post($comment_post_id);
            // Post Friendly URL (Slug)
            $post_slug = $post->post_name;
            $post_title = $post->post_title;

            // Logged in User
            $User_ID = get_current_user_id();
            $current_user = get_userdata($User_ID);
            $user_email = $current_user->user_email;
            ?>

                <script type="text/javascript">
                    var comment_metadata = {
                        slug: '<?php echo $post_slug; ?>',
                        post_title: '<?php echo $post_title; ?>'
                    };
                    Intercom('trackEvent', 'Commented', comment_metadata);
                </script>

            <?php
    }

}

I have also assumed that the other required tags are not firing on this hook so I tried to include them too like so but it still didn’t work.

wp_enqueue_script( 'intercom-widget', plugin_dir_url(__FILE__) . 'intercom-widget.js', array('jquery') );
wp_enqueue_script( 'intercom-tag-user', plugin_dir_url(__FILE__) . 'intercom-tag-user.js', array('jquery') );

I have been at this since yesterday and I can’t figure out why my code is not working. Hopefully a more established developer would help me understand the issue.

Thanks!

Read more here: Intercom Event on WordPress Comment Post


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: