Shoot! I totally forgot I'd eaten a couple of hours ago. If I eat this right now, I'll gain weight.
Is it okay to use "a couple of hours ago" with the past perfect in this case since it just happened?
Yes.
New words, one handy idiom, and a 2-minute quiz — delivered to your inbox to keep your streak alive.
"I totally forgot" is (as I understood you intended it) only momentarily in the past, i.e. the person has only just that moment remembered or realised. Therefore, "ago" (relative to now) seems acceptable to me. You wouldn't use "ago" relative to something further back in the past though.