ActiveCampaign Event Tracking Using Learndash Hook in Wordpress

321 Views Asked by At

I'm a bit of a newbie to PHP and wordpress (the dreaded opener), and I'm trying to add some functionality to an e-learning site I've built out using the LearnDash plugin for Wordpress.

One of the ways I'm tracking user progress is with ActiveCampaign. The site tracking is installed and working great, but I'm trying to get an event to be logged in AC when a user completes a course. There's a LearnDash hook available for this (https://www.learndash.com/support/docs/developers/hooks-and-filters/), and the actual event code is available from the Active Campaign tracking page.

I've ended up with the following code:


//learndash hook:
add_action("learndash_course_completed", sendEmail,5,1);

//defining user's email to use in tracking event code:
function sendEmail(){
    $current_user = wp_get_current_user();
    $userEmail = $user->user_email;

//Event Tracking code as per the Active Campaign Page - Account info omitted

    $curl = curl_init();
       curl_setopt($curl, CURLOPT_URL, "https://trackcmp.net/event");
       curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
       curl_setopt($curl, CURLOPT_POST, true);
       curl_setopt($curl, CURLOPT_POSTFIELDS, array(
       "actid" => "ACCOUNT ID NUMBER",
       "key" => "TRACKING KEY NUMBER",
       "event" => "EVENT NAME",
       "eventdata" => "User Has Completed the course",
       "visit" => json_encode(array(
               // If you have an email address, assign it here.
               "email" => $userEmail,
           )),
       ));

       $result = curl_exec($curl);
       if ($result !== false) {
           $result = json_decode($result);
           if ($result->success) {
               echo 'Success! ';
           } else {
               echo 'Error! ';
           }

           echo $result->message;
       } else {
           echo 'cURL failed to run: ', curl_error($curl);
}
};

It's not firing when the user completes a course and I'm not sure what I've done wrong. Probably something really obvious. Any thoughts?

Thanks!

1

There are 1 best solutions below

0
On

I found the problem: I'd called the $user variable, when it was actually defined as $current-user.

Changing that fixed the problem.