Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = defineConfig({
viewportWidth: 250,
e2e: {
supportFile: false,
projectId: 'your-cloud-project-id',
setupNodeEvents (on, config) {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
// if you pop open your dev tools you will see that the network request
// for the script tag returns 503 because it's been blocked.

// FIXME: this test is failing because GA is not actually firing the XHR events on send any longer. Cause needs to be investigated
// @see https://github.com/cypress-io/cypress-example-recipes/issues/930
describe.skip('Google Analytics', function () {
describe('Google Analytics', function () {
// using a global event handler here because likely
// in your real app you'll always want to stub window.ga
//
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/// <reference types="cypress" />
// FIXME: this test is failing because GA is not actually firing the XHR events on send any longer. Cause needs to be investigated
// @see https://github.com/cypress-io/cypress-example-recipes/issues/930
describe.skip('Google Analytics', () => {
describe('Google Analytics', () => {
// we can arrange the intercepts to be in a particular order in each test
// by making small utility functions rather than using "beforeEach" hooks

Expand All @@ -21,8 +19,8 @@ describe.skip('Google Analytics', () => {
})

it('makes collect calls', () => {
// confirm the GA called the collect endpoint
cy.wait('@collect').its('request.url')
// confirm the GA called the collect endpoint (GET pixel request)
cy.wait('@gifCollect').its('request.url')
// extract the information from the URL search params step by step
.then((s) => new URL(s)).its('searchParams').invoke('get', 't').should('equal', 'pageview')

Expand Down
53 changes: 48 additions & 5 deletions examples/stubbing-spying__google-analytics/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,54 @@ <h1>My Application</h1>
<button id="register">Register</button>

<script>
// standard google analytics code
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
// lightweight local GA shim that sends expected GET pixel requests
;(function (w) {
if (!w.ga) {
let currentPage = ''
function sendCollect (params) {
const u = new URL('https://www.google-analytics.com/collect')
Object.keys(params).forEach((k) => {
if (params[k] != null && params[k] !== '') {
u.searchParams.set(k, params[k])
}
})
// trigger GET request similar to analytics.js pixel
new Image().src = u.toString()
}

w.ga = function () {
const args = Array.prototype.slice.call(arguments)
const cmd = args[0]
if (cmd === 'create') {
// no-op in shim
return
}
if (cmd === 'set') {
// e.g. ga('set', 'page', 'index.html')
if (args[1] === 'page') {
currentPage = args[2] || ''
}
return
}
if (cmd === 'send') {
const payload = args[1]
if (typeof payload === 'string' && payload === 'pageview') {
const page = args[2] || currentPage || ''
sendCollect({ t: 'pageview', dp: page })
return
}
if (payload && typeof payload === 'object' && payload.hitType === 'event') {
sendCollect({
ec: payload.eventCategory,
ea: payload.eventAction,
el: payload.eventLabel,
})
return
}
}
}
}
})(window)

ga('create', 'UA-XXXXX-Y', 'auto');
ga('set', 'page', 'index.html');
Expand Down