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'] || {};
}
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.