forked from SonarSource/sonarqube
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SONARCLOUD-615 Add Google Tag Manager
- Loading branch information
1 parent
1fd7155
commit 5a5f331
Showing
9 changed files
with
193 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
server/sonar-web/src/main/js/app/components/__tests__/PageTracker-test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* SonarQube | ||
* Copyright (C) 2009-2019 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
import { mount } from 'enzyme'; | ||
import * as React from 'react'; | ||
import { PageTracker } from '../PageTracker'; | ||
import { mockLocation, mockRouter } from '../../../helpers/testMocks'; | ||
|
||
jest.useFakeTimers(); | ||
|
||
beforeEach(() => { | ||
jest.clearAllTimers(); | ||
|
||
(window as any).dataLayer = []; | ||
|
||
document.getElementsByTagName = jest.fn().mockImplementation(() => { | ||
return [document.body]; | ||
}); | ||
}); | ||
|
||
it('should not trigger if no analytics system is given', () => { | ||
shallowRender(); | ||
|
||
expect(setTimeout).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should work for Google Analytics', () => { | ||
const wrapper = shallowRender({ trackingIdGA: '123' }); | ||
const instance = wrapper.instance(); | ||
instance.trackPage(); | ||
|
||
expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 500); | ||
}); | ||
|
||
it('should work for Google Tag Manager', () => { | ||
const wrapper = shallowRender({ trackingIdGTM: '123' }); | ||
const instance = wrapper.instance(); | ||
const dataLayer = (window as any).dataLayer; | ||
|
||
expect(dataLayer).toHaveLength(1); | ||
|
||
instance.trackPage(); | ||
|
||
expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 500); | ||
|
||
jest.runAllTimers(); | ||
|
||
expect(dataLayer).toHaveLength(2); | ||
}); | ||
|
||
function shallowRender(props: Partial<PageTracker['props']> = {}) { | ||
return mount<PageTracker>( | ||
<PageTracker | ||
location={mockLocation()} | ||
params={{}} | ||
router={mockRouter()} | ||
routes={[]} | ||
{...props} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* SonarQube | ||
* Copyright (C) 2009-2019 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
// The body of the `gtm` function comes from Google Tag Manager docs; let's keep it like it was written. | ||
// @ts-ignore | ||
// prettier-ignore | ||
// eslint-disable-next-line | ||
const gtm = id => (function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});const f=d.getElementsByTagName(s)[0], j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);}(window,document,'script','dataLayer',id)); | ||
|
||
module.exports = { gtm }; |