冻结重定向柏树

发布于 2025-02-05 20:22:47 字数 2209 浏览 3 评论 0原文

我想测试GA4产品点击事件。为此,我在数据层中写下所需的数据,我想看看正确的数据是否在数据层中。但是,当我在柏树中单击产品时,重定向的速度比测试可以读取数据层的速度快。有什么办法可以暂停或冻结重定向?

在这里,数据层中的期望网数据:

                select_item: {
                    event: 'select_item',
                    ecommerce: {
                        item_name: 'Artiklename',
                        item_id: '000000',
                        price: 1.19,
                        currency: 'EUR',
                        item_brand: 'Brand',
                        item_category: 'category',
                        item_category2: 'category2',
                        item_category3: 'category3',
                        item_category4: 'category4',
                        index: 1,
                        quantity: 1,
                        item_list_name: "List Name"
                    },
                },

这里实际测试:

context('Google Analytics 4: should track select_item event', function () {
    it('should track select item on search page', function () {
        cy.getTrackingData('expressShippingArticle', 'select_item').then(
            (expectedSelectItemEvent) => {

                // act
                cy.visitWithBasicAuth(
                    routes.category.resultList(
                        '000/SomeArticle'
                    )
                )
                
                //assert
                cy.getSpecificEventFromDataLayer('select_item').then(
                    (event) => {
                        cy.wrap(event).should('not.exist')
                    }
                )
                
                // act
                cy.get(selectors.resultList.productInResultList)
                    .first()
                    .click()
                
                cy.getSpecificEventFromDataLayer('select_item').then(
                    (actualSelectItemEvent) => {
                        cy.wrap(actualSelectItemEvent, { timeout: 0 }).should(
                            spok(expectedSelectItemEvent)
                        )
                    }
                )
            }
        )
    })    
})

I would like to test a GA4 product click event. For this I write the required data in the data layer and I want to see if the correct data is in the data layer. However, when I click on the product in Cypress, the redirect is faster than the test can read the data layer. Is there any way I can pause or freeze the redirect?

Here the expectet data in the Datalayer:

                select_item: {
                    event: 'select_item',
                    ecommerce: {
                        item_name: 'Artiklename',
                        item_id: '000000',
                        price: 1.19,
                        currency: 'EUR',
                        item_brand: 'Brand',
                        item_category: 'category',
                        item_category2: 'category2',
                        item_category3: 'category3',
                        item_category4: 'category4',
                        index: 1,
                        quantity: 1,
                        item_list_name: "List Name"
                    },
                },

Here the actual Test:

context('Google Analytics 4: should track select_item event', function () {
    it('should track select item on search page', function () {
        cy.getTrackingData('expressShippingArticle', 'select_item').then(
            (expectedSelectItemEvent) => {

                // act
                cy.visitWithBasicAuth(
                    routes.category.resultList(
                        '000/SomeArticle'
                    )
                )
                
                //assert
                cy.getSpecificEventFromDataLayer('select_item').then(
                    (event) => {
                        cy.wrap(event).should('not.exist')
                    }
                )
                
                // act
                cy.get(selectors.resultList.productInResultList)
                    .first()
                    .click()
                
                cy.getSpecificEventFromDataLayer('select_item').then(
                    (actualSelectItemEvent) => {
                        cy.wrap(actualSelectItemEvent, { timeout: 0 }).should(
                            spok(expectedSelectItemEvent)
                        )
                    }
                )
            }
        )
    })    
})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神经大条 2025-02-12 20:22:48

不确定这是否适用于GA4,但是如果要延迟网络请求/响应,请添加使用延迟选项的截距

cy.intercept(redirect-url, (req) => {
  req.reply(res => {
    res.setDelay(2000)
  })
})

// act
cy.get(selectors.resultList.productInResultList).first().click()
...

Not sure if this works with GA4, but if you want to delay a network request/response add an intercept with delay option

cy.intercept(redirect-url, (req) => {
  req.reply(res => {
    res.setDelay(2000)
  })
})

// act
cy.get(selectors.resultList.productInResultList).first().click()
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文