Hi,
I’d like to pick up this topic. I would also find it interesting to track topic + reply creations via Google Analtics goal tracking.
My Google research was unsuccessful and I’m not a developer. Does anyone maybe already have a solution or can point me in the right direction?
Thanks in advance!
I went through the documentation of adding actions (https://codex.bbpress.org/step-by-step-guide-to-setting-up-a-bbpress-forum-part-4/) and then I looked up the places within the code for the following 3 events:
– user registration
– topic creation
– reply creation
I see, that I could add code before and after the “submit” button, but that’s not exactly what I need.
I need to add an function call for ga.js directly to the button.
Example (this is the code line for the “submit new topic” button):
<button type=”submit” tabindex=”<?php bbp_tab_index(); ?>” id=”bbp_topic_submit” name=”bbp_topic_submit” class=”button submit”><?php _e( ‘Submit’, ‘bbpress’ ); ?></button>
this must become:
<button type=”submit” tabindex=”<?php bbp_tab_index(); ?>” id=”bbp_topic_submit” name=”bbp_topic_submit” class=”button submit” onClick=”_gaq.push([‘_trackEvent’, ‘Forum’, ‘New Topic’]);”><?php _e( ‘Submit’, ‘bbpress’ ); ?></button>
How can I achieve this?
Nice! I think I found a solution. This page describes how to add an event listener to a button:
https://developers.google.com/analytics/devguides/collection/analyticsjs/events
I’ll try this out later and then I’ll let you, the world, know how to solve it 🙂
I figured it out.
1st way: count clicks of “submit” button:
//custom function
function custom_trackEvent_newTopic ()
{
echo '
<script>
addListener(document.getElementById(\'bbp_topic_submit\'), \'click\', function() {
ga(\'send\', \'event\', \'forum\', \'btn_click_new topic\');
});
function addListener(element, type, callback) {
if (element.addEventListener) element.addEventListener(type, callback);
else if (element.attachEvent) element.attachEvent(\'on\' + type, callback);
}
</script>
';
}
add_action ('bbp_theme_after_topic_form_submit_button', 'custom_trackEvent_newTopic') ;
2nd way: count actual topic creation
This would be more precise, but I didn’t figure it out yet. Actually this should work:
function custom_trackEvent_newTopic ()
{
window.ga('send', 'event', 'forum', 'new topic');
}
add_action ('bbp_new_topic', 'custom_trackEvent_newTopic') ;
…but it doesn’t.
Fatal error: Call to undefined function ga() in /…/themes/…/functions.php on line 1594
Somehow the function can’t be referenced that way. 🙁