When I click on button, I should send into datalayer information, but I don't know how to do it, because I'm using Angular 6, so I need to use Typescript and window.dataLayer.push not working and give me this error
Form
<form>
<div class="radio">
<input value="Yes" id="radio-1" [(ngModel)]="answer" name="radio" type="radio">
<label class="radio-label rob-l" for="radio-1">Yes</label>
</div>
<div class="radio">
<input value="No" id="radio-2" [(ngModel)]="answer" name="radio" type="radio">
<label class="radio-label rob-l" for="radio-2">No</label>
</div>
</div>
<div class="btn">
<button (click)="Next()" type="submit">Next question</button>
</div>
</form>
And I want to recieve smth like that
Next(){
if ((this.path == 1) && (this.answer === "Yes" || this.answer === "No"))
{
// window.dataLayer = window.dataLayer || [];
// window.dataLayer.push({
// 'event': 'answer',
// 'answer': this.answer
// });
this.path++;
this.answer = "";
}
}
Problem solved with this code:
window['dataLayer'] = window['dataLayer'] || [];
window['dataLayer'].push({
'event': 'Answer',
'failedText': this.answer
});
ngOnInit() {
window['dataLayer'] = window['dataLayer'] || {};
}
The syntax for sending an event with dataLayer.push() is as follows: dataLayer.push({'event': 'event_name'});
The return value, assuming you are referring to when you pasted the code into the console, indicates whether a GTM tag fired in response to the push. "true" means that no tags fired, and "false" means that a tag fired.
Google Tag Manager functions best when deployed alongside a data layer. A data layer is a JavaScript object that is used to pass information from your website to your Tag Manager container. You can then use that information to populate variables and activate triggers in your tag configurations.
You can can declare global Window interface with dataLayer
property on top of yout component/service ect., like this:
// Declare gTM dataLayer array.
declare global {
interface Window { dataLayer: any[]; }
}
And then just use that property in the code of your component without any errors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With