Triggering JS code when a Data Layer variable gets populated?

Is there any solution for executing some extension code based off a data layer variable getting populated? I'm missing out on some events that look for utag.data.order_id, and trying to figure out the best way to avoid missing them.

It could be a timing issue, but if there's a way to trigger based off that order_id getting populated, I'd be all set.

When you say 'hook onto' it, what exactly do you mean? I know I can reference it. My current JS code checks for a regex match on URL pathname, as well as looking for utag.data.order_id that is not blank or null or undefined.

It works 70% of the time, but seems to miss out on some. I figure it's a timing issue, so I need some way to trigger off the population of that value.

Re: Triggering JS code when a Data Layer variable gets populated?

My initial solution would have been to use a set data values extension that executes when order_id is populated.It seems you've already thought of that but you're still experiencing some issues.Could you send a screenshot of your current code or extension?

Re: Triggering JS code when a Data Layer variable gets populated?

‎09-11-201711:43 PM - last edited on ‎09-12-201708:00 AM by kathleen_jo

Since TealumIQ is working page by page the eCommerce extension variables will populate also on a page basis. Using the eCommerce variables directly, like _corder for example, has not worked reliably for me. I guess that is a timing issue. Instead I just use fields I know are populated/not populated once the page loads, like the field you have mapped to _corder. If you trigger on that you could pop your js code into an extension or even easier in a generic tag, @nickmv.

(To try to trigger whilst on a page, when a specific variable is actually populated I would think is pretty much impossible and unpredictable.)

My initial solution would have been to use a set data values extension that executes when order_id is populated.It seems you've already thought of that but you're still experiencing some issues.Could you send a screenshot of your current code or extension?

Thanks,Jonathan

Remember to give me a kudo if you like my post! Accepting my post as a solution is even better! Also remember that search is your friend.

Re: Triggering JS code when a Data Layer variable gets populated?

Sorry, I've been a bit busy and also trying some of the methods listed here.

My implementation is basically a JS script that inserts a script into the top of <body> using a jQuery prepend function, referencing a JS variable that is essentially the entire script, 'escaped.' Then later on in the main script (not the one inserted), I have logic that checks for values and sends push events as necessary, using methods and functions that were defined in the inserted script.

The logic for sending off a push event for a purchase is currently as follows (whole thing executes on DOM Ready):

I don't believe there are any issues with it sending blanks. Instead, the only issue is that it doesn't seem to always be picking up transactions, since GA sometimes shows up to 25-30% more transactions (and their IDs). That suggests a timing issue.

Currently I am trying a method that was suggested above, and that is to execute a 1000ms setTimeout on the checks for these values. I'm pretty sure this is all a timing issue, but I'm just not totally sure the best way to tackle it, since there's no such thing as a 'variable population listener'.

Another thought I've had (but have not tested yet) is to split this out into 2 scripts --- the script that inserts the script at top of body, and then another script that inserts the logic checks at the bottom of <body>. I don't think that'll work any better than the 1000ms setTimeout though.