I am using a variable in GTM data layer which retrieves its value from Liferay rest API call.
var openId;
AUI().ready('aui-base', function(A){
Liferay.Service(
'/user/get-user-by-id',
{
userId: Liferay.ThemeDisplay.getUserId()
},
function(obj) {
openId=obj.openId;
}
);
});
Here the openId variable is being used in GTM dataLayer,where it is not available and shows as undefined.
The data layer is defined as below:
dataLayer = [{
"page" : Liferay.currentURL
,"pageType" : instrDetail
,"user" : {
"type" : userType
,"accountId" : openId
}
}];
and in the end,we have the GTM block,defining the tag manager script block.
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXXX');
Now,when I try to access the openId variable in dataLayer,it shows as undefined,whereas when I try to access the same one browser console,it displays the correct value.