optional arguments to utag.view()

TroyP
Bronze Contributor
Bronze Contributor TroyP

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?

3 REPLIES 3

optional arguments to utag.view()

ahrasch
Silver Contributor
Silver Contributor ahrasch

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.

Highlighted

optional arguments to utag.view()

TroyP
Bronze Contributor
Bronze Contributor TroyP

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.

optional arguments to utag.view()

Tealium Employee

@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