Reply
Highlighted
Visitor
Posts: 2
Registered: ‎09-15-2016
Accepted Solution

optional arguments to utag.view()

I see that utage.view() has to optional arguements, a data_object and a callback function.  However, I don't see the callback function executed if the data_object is empty.

This call does not log anything.

utag.view(function(){console.log('I am in the callback')}) 

This call does log, "I am in the callback"

utag.view({}, function(){console.log('I am in the callback')})

Is this behavior a bug or desired behavior?

Reply
0 Kudos
Visitor
Posts: 34
Registered: ‎03-09-2016

Re: optional arguments to utag.view()

Look at the signature of the utag.view() function (https://community.tealiumiq.com/t5/JavaScript-utag-js/Page-Tracking/ta-p/15563): 

utag.view(data_object, callback, [uid_array]);

In your first example you pass the function as the data object and not as the callback.

Reply
0 Kudos
Visitor
Posts: 2
Registered: ‎09-15-2016

Re: optional arguments to utag.view()

However, if the data_object is optional as the documentation says then the execution should check if the argument is a functon and treat it as a callback.

Reply
0 Kudos
Tealium Employee
Posts: 33
Registered: ‎08-15-2016

Re: optional arguments to utag.view()

[ Edited ]

@TroyP - it's true that some JS libraries and newer features (thinking of Promises) will do type-based argument checks like you seem to be expecting, which allows developers to omit (preceding) optional arguments completely but still pass a callback.

 utag.view() doesn't do that - if you want to omit the first argument but send the second, you need to put a falsey placeholder in for the first:

 

utag.view(null, function callback(){console.log("Callback fired");});

 

That same principle also holds true for other utag functions with optional arguments, to the best of my knowledge.

Hope that helps?

Best, Caleb