Counting the amount of times movement occurs using the tracker edge firmware

I am currently trying to customize the tracker-edge firmware (version 19) to increment a variable every time the device is woken by the IMU, specifically movement being the trigger and not high g, so imu_m and not imu_g. I am working with the Tracker SoM evaluation kit.

It is possible to do this from the "main.cpp" file alone? I have one idea discussed below but it involves modifying another file which I would prefer not to do.

I have read a number of posts and the most promising information I found so far is from this post: Customizing the IMU triggers.
However I still haven't managed to implement what I need.

This is what I gathered would work:
(The below code is from the "tracker_motion.cpp" file, but I added the variable "moveCount", this variable is declared in the "main.cpp" and is made accessible to the lines of code below via the "extern" keyword)

void TrackerMotion::loop()
{
    MotionEvent motion_event;
    size_t depth = MotionService::instance().getQueueDepth();
    do {
        MotionService::instance().waitOnEvent(motion_event, 0);
        switch (motion_event.source)
        {
            case MotionSource::MOTION_HIGH_G:
                TrackerLocation::instance().triggerLocPub(Trigger::NORMAL, "imu_g");
                break;
            case MotionSource::MOTION_MOVEMENT:
                TrackerLocation::instance().triggerLocPub(Trigger::NORMAL,"imu_m");
                moveCount++;    //    here is my added variable
                break;
        }
    } while (--depth && (motion_event.source != MotionSource::MOTION_NONE));
}

this code has unfortunately not worked how I hoped so after careful deliberation, I decided to turn to the community. Any help would be much appreciated.

It looks like that should work. How do you know it's not working? Is that line of code never executed, or is moveCount getting reset to 0? Is the variable retained?

The line is executed and and moveCount gets incremented but at a much higher rate than expected. If I wake the device by moving it, say 10 times between publishes (every 15 minutes in my case), and then publish the moveCount value, I get numbers in the range of 30 to 40. The method definitely proves a wake up has occured, however it does not accurately show how many times the device has been woken up. This information comes from testing the code as shown

As for the variable being retained, I will have to come back to you on that. I am relatively new to the scene and will need too look that up.

Don't worry about retained, it's something completely different.

If you want the filtered value which matches the number of publishes, you'll probably need to increment the counter here instead:

Thank you @rickkas7, I will implement this ASAP