1 year ago
#330442
Joris Alexandre
Getting data from a dynamic object in an event of my Datalayer
I'm requesting your help, I have trouble to catch data from a specific event in my datalayer.
So, I have a pop-up survey on my website. I'm pushing the answers in my GTM datalayer as you can see on the screenshot below :
In the survey, there are 2 textarea who doesn't appears in the datalayer if the user don't fill them. If the user fill the text area, you have the following result in the datalayer:
A user can fill 0, 1 or 2 textareas.
Note that the object userData.data doesn't have the same length according if the user is filling textareas or not.
My goal is to send the data I'm receving in the UserData.data object to Google Analytics.
In GA I want to receive data as following example:
- eventCategory: Campaign + eventLabel we can see on the screenshot
- eventAction: Key data from userData.data (ex: First_Time_On_Site or Purchasing_Factor or Type_content, etc...)
- eventLabel: Value data from userData.data (ex if eventAction = First_Time_On_Site I want to receive the answer of the question so here "Yes" or "No" according of what we get from the datalayer).
I'm using the following code to get the data, but I'm not getting what I want.
// When a campaign is started listen for answers, otherwise we're done
if (action != "Campaign:Open") {
return;
}
window.addEventListener("message", function (event) {
// Listen to messages from the Usabilla Cloudfront domain
if (!/d6tizftlrpuof\.cloudfront\.net/.test(event.origin)) {
return;
}
try {
var data = JSON.parse(event.data);
// On the final page
// if data type is not STRING then join and make a string before sending to GA
if (data.type === "pageSwitch" && data.end) {
for (var key in data.data) {
if (typeof data.data[key] !== "string") {
data.data[key] = data.data[key].join(", ");
}
ga(
"ga_trackername" + ".send",
"event",
"Usabilla Campaign - " + label, // event category
key, // event action
data.data[key] // event label
);
}
}
} catch (e) {
// Ignore errors, usually JSON decode problems
}
});
}
);
// End Sending event to GA
At the moment I'm receiving the data from the Action/Category/Label present in my datalayer event. My goal is to receive the data from the object userData.data. Does anyone have any idea to update this code to send the correct data?
Thanks a lot for your help here.
javascript
json
google-analytics
google-tag-manager
google-datalayer
0 Answers
Your Answer