I have a WordPress website where I use LearnDash with multiple courses, lessons and quizzes.
I? am looking to set up GTM in order to better track what my visitors are doing. There is a container in place and from the console, I am able to use the datalayer.push() to push custom events to my GTM.
I would like to pass certain data to my GTM for when specific hooks are fired, for example, when someone completes a course or a lesson. Luckily there are action hooks for this provided by?
So far, I have an implementation of SSE in place which sends the data from database to frontend but it is unreliable. What other approaches can I use for this?
Currently saving the Data to database when a hook is triggered, then using SSE (server sent events) to push this data to frontend. However, due to the persistent connection, this causes my CPU to crash so its not so reliable, especially when there are lots of users.
This method does work, as a new event is created, saved to the database, then sent to the frontend from where I can then use datalayer.push to add this to GTM, but again, it is unreliable and crashes the website from time to time.
2
Answers
Yeah I was thinking there is some sort of mistake in the code, however I have been over this for many days and its starting to drive me insane. This is the PHP code that I use for the SSE events.
This is the frontend code
I have taken extra caution to prevent multiple SSE connections being open by introducing locks into the PHP. The concurrency lock is to prevent multiple connections per users in case they open multiple tabs.
Perhaps there is something wrong in my code that is causing it to crash? At this point, I am not sure as I feel I have tried everything. If anyone knows, then I would appreciate some guidance.
First of all, your crashing issues are unrelated. You probably made some architectural mistakes that increase complexity dramatically at some step where no complexity is required really.
The way you described it is pretty much the only way to send events to front-end GTM. There’s a server-side GTM that doesn’t require front-end as a middleware, but most likely you don’t need that level of complexity.
If the only thing you need to track is people finishing courses, you can just track something like the course completion page maybe parsing DOM from the GTM to detect it if there’s no clear url pattern. Doing this kind of truly trivial tracking through backend sounds like a huge mistake.