Google Gtag or DataLayer Push not working within action hook
Question
I used to use a function just fine but recently, perhaps because I corrected my implementation of Google Tag Manger and perhaps for other reason, I can’t seem to use this code which is hooked to a click of a button that completes lessons within WP Courseware. This button is in a page where the GTM code is working fine.
Here is the DataLayer version:
add_action('wpcw_user_completed_unit', 'siv_unit_completed_ga', 50, 3);
function siv_unit_completed_ga($userID, $unitID, $unitParentData){
$current_user = get_userdata($userID);
$user_email = $current_user->user_email;
$course_post = get_post($unitID);
// Post Friendly URL (Slug)
$course_post_slug = $course_post->post_name;
//$course_post_title = $course_post->post_title;
$module_id_after_order = $unitParentData->module_order;
$course_id = $unitParentData->course_id;
?>
<!-- Start Adding of DataLayer Data to GTM -->
<script>
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'event': "Lesson Completed",
'wp_user_email': "<?php echo $user_email ?>",
'wp_user_id': "<?php echo $userID ?>",
'course_id': "<?php echo $course_id ?>",
'module': "<?php echo $module_id_after_order ?>",
'unit_id': "<?php echo $unitID ?>",
'unit_slug': "<?php echo $course_post_slug ?>"
});
</script>
<!-- End Adding of DataLayer Data to GTM -->
<?php
}
Similarly, here is the previous code that used to work (Gtag):
add_action('wpcw_user_completed_unit', 'siv_analytics_course_unit_completed_ga', 50, 3);
function siv_analytics_course_unit_completed_ga($userID, $unitID, $unitParentData){
$course_post = get_post($unitID);
// Post Friendly URL (Slug)
$course_post_slug = $course_post->post_name;
//$course_post_title = $course_post->post_title;
$module_id_after_order = $unitParentData->module_order;
$course_id = $unitParentData->course_id;
?>
<!-- Google Analytics Goal: Completed Lesson -->
<script type="text/javascript">
gtag('event', 'Completed', {
'event_category': 'Lesson',
'event_label': 'C<?php echo $course_id ?>-M<?php echo $module_id_after_order ?>-U<?php echo $unitID ?>:<?php echo $course_post_slug; ?>',
});
</script>
<?php
}
Would appreciate any feedback/recommendations to help solve this.
Thanks!
0
actions, functions, google-analytics, hooks, plugin-development
3 years
2019-10-28T02:00:06-05:00
2019-10-28T02:00:06-05:00 0 Answers
92 views
0
Leave an answer