I'm trying to trigger GA events using Nuxt. It's successfully working but it's only returning the first one it runs into because of document.querySelector
rather than querySelectorAll
. I have tried the spread operator and a for()
but still no luck - the compiler just says cannot read property txt of undefined
.
Here's my v-for:
<ul>
<li class="li-item" v-for="item in items" :key="item.id" @click="doGA" :data-txt="item.name">
{{ item.name }}
</li>
</ul>
<ListDetails
:items="[
{
name: 'Lorem',
},
{
name: 'Ipsum',
},
{
name: 'Dolor',
},
]"
/>
methods: {
doGA(){
this.$ga.event({
eventCategory: "Ext links",
eventAction: "click",
//this.dataset.txt does not work below here either
eventLabel: document.querySelectorAll('.li-item').dataset.txt;
});
}
}
So when a user clicks 'Ipsum' it's returning 'Lorem' in GA and likewise for 'Dolor' it will do the same. It is because I have used document.querySelector
rather than selectorAll
as I have said above, but I can't get all()
to work properly.
As we know querySelector
will return this first one the browser finds.