Skip to content

Commit bccc2a3

Browse files
authored
🐛 [Story analytics] Fix prerender analytics not firing (#37975)
* Added tasts * Undo * Removed timeout of 10s * Reverted past change and added visibility wait * Fixed unit tests * Remove unused import in test * Test analytics not sent if not visible * Fixed test index value
1 parent ba3e405 commit bccc2a3

File tree

4 files changed

+69
-17
lines changed

4 files changed

+69
-17
lines changed

extensions/amp-story/1.0/story-analytics.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {triggerAnalyticsEvent} from '#utils/analytics';
88
import {StateProperty, getStoreService} from './amp-story-store-service';
99
import {getVariableService} from './variable-service';
1010

11-
import {registerServiceBuilder} from '../../../src/service-helpers';
11+
import {getAmpdoc, registerServiceBuilder} from '../../../src/service-helpers';
1212

1313
/** @const {string} */
1414
export const ANALYTICS_TAG_NAME = '__AMP_ANALYTICS_TAG_NAME__';
@@ -127,11 +127,15 @@ export class StoryAnalyticsService {
127127
triggerEvent(eventType, element = null) {
128128
this.incrementPageEventCount_(eventType);
129129

130-
triggerAnalyticsEvent(
131-
this.element_,
132-
eventType,
133-
this.updateDetails(eventType, element)
134-
);
130+
getAmpdoc(this.element_)
131+
.whenFirstVisible()
132+
.then(() =>
133+
triggerAnalyticsEvent(
134+
this.element_,
135+
eventType,
136+
this.updateDetails(eventType, element)
137+
)
138+
);
135139
}
136140

137141
/**

extensions/amp-story/1.0/test/test-amp-story-embedded-component.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import {LocalizationService} from '#service/localization';
55

66
import * as analyticsApi from '#utils/analytics';
77

8-
import {registerServiceBuilder} from '../../../../src/service-helpers';
8+
import {
9+
getAmpdoc,
10+
registerServiceBuilder,
11+
} from '../../../../src/service-helpers';
912
import {AmpStoryEmbeddedComponent} from '../amp-story-embedded-component';
1013
import {
1114
Action,
@@ -224,20 +227,22 @@ describes.realWin('amp-story-embedded-component', {amp: true}, (env) => {
224227
expect(tooltipTextEl.textContent).to.equal('google.com');
225228
});
226229

227-
it('should fire analytics event when entering a tooltip', () => {
230+
it('should fire analytics event when entering a tooltip', async () => {
228231
fakePage.appendChild(clickableEl);
229232
storeService.dispatch(Action.TOGGLE_INTERACTIVE_COMPONENT, {
230233
element: clickableEl,
231234
state: EmbeddedComponentState.FOCUSED,
232235
});
233236

237+
await getAmpdoc(win.document).whenFirstVisible();
238+
234239
expect(analyticsTriggerStub).to.be.calledWith(
235240
parentEl,
236241
StoryAnalyticsEvent.FOCUS
237242
);
238243
});
239244

240-
it('should send data-var specified by publisher in analytics event', () => {
245+
it('should send data-var specified by publisher in analytics event', async () => {
241246
addAttributesToElement(clickableEl, {
242247
'data-vars-tooltip-id': '1234',
243248
});
@@ -248,6 +253,8 @@ describes.realWin('amp-story-embedded-component', {amp: true}, (env) => {
248253
state: EmbeddedComponentState.FOCUSED,
249254
});
250255

256+
await getAmpdoc(win.document).whenFirstVisible();
257+
251258
expect(analyticsTriggerStub).to.be.calledWithMatch(
252259
parentEl,
253260
StoryAnalyticsEvent.FOCUS,
@@ -257,7 +264,7 @@ describes.realWin('amp-story-embedded-component', {amp: true}, (env) => {
257264
);
258265
});
259266

260-
it('should fire analytics event when clicking on the tooltip of a link', () => {
267+
it('should fire analytics event when clicking on the tooltip of a link', async () => {
261268
fakePage.appendChild(clickableEl);
262269
storeService.dispatch(Action.TOGGLE_INTERACTIVE_COMPONENT, {
263270
element: clickableEl,
@@ -273,13 +280,15 @@ describes.realWin('amp-story-embedded-component', {amp: true}, (env) => {
273280

274281
tooltip.click();
275282

283+
await getAmpdoc(win.document).whenFirstVisible();
284+
276285
expect(analyticsTriggerStub).to.be.calledWith(
277286
parentEl,
278287
StoryAnalyticsEvent.CLICK_THROUGH
279288
);
280289
});
281290

282-
it('should fire analytics event when clicking on the tooltip of a tweet', () => {
291+
it('should fire analytics event when clicking on the tooltip of a tweet', async () => {
283292
clickableEl = win.document.createElement('amp-twitter');
284293
addAttributesToElement(clickableEl, {
285294
'data-tweetid': '1166723359696130049',
@@ -300,6 +309,8 @@ describes.realWin('amp-story-embedded-component', {amp: true}, (env) => {
300309

301310
tooltip.click();
302311

312+
await getAmpdoc(win.document).whenFirstVisible();
313+
303314
expect(analyticsTriggerStub).to.be.calledWith(
304315
parentEl,
305316
StoryAnalyticsEvent.FOCUS

extensions/amp-story/1.0/test/test-amp-story-share.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import {Services} from '#service';
44

55
import * as analyticsApi from '#utils/analytics';
66

7-
import {registerServiceBuilder} from '../../../../src/service-helpers';
7+
import {
8+
getAmpdoc,
9+
registerServiceBuilder,
10+
} from '../../../../src/service-helpers';
811
import {AmpStoryShare} from '../amp-story-share';
912
import {
1013
Action,
@@ -94,7 +97,7 @@ describes.realWin('amp-story-share', {amp: true}, (env) => {
9497
});
9598
});
9699

97-
it('should send correct analytics tagName and eventType when opening the share menu', () => {
100+
it('should send correct analytics tagName and eventType when opening the share menu', async () => {
98101
analyticsTriggerStub = env.sandbox.stub(
99102
analyticsApi,
100103
'triggerAnalyticsEvent'
@@ -103,6 +106,8 @@ describes.realWin('amp-story-share', {amp: true}, (env) => {
103106

104107
storeService.dispatch(Action.TOGGLE_SHARE_MENU, true);
105108

109+
await getAmpdoc(win.document).whenFirstVisible();
110+
106111
// tagName should be amp-story-share-menu as per extensions/amp-story/amp-story-analytics.md
107112
expect(analyticsTriggerStub).to.be.calledWith(
108113
ampStory,

extensions/amp-story/1.0/test/test-story-analytics.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import * as analytics from '#utils/analytics';
22

3+
import {getAmpdoc} from 'src/service-helpers';
4+
35
import {Action, getStoreService} from '../amp-story-store-service';
4-
import {StoryAnalyticsService} from '../story-analytics';
6+
import {getAnalyticsService} from '../story-analytics';
57

68
describes.realWin('amp-story-analytics', {amp: true}, (env) => {
79
let el;
@@ -10,11 +12,12 @@ describes.realWin('amp-story-analytics', {amp: true}, (env) => {
1012
beforeEach(() => {
1113
const {win} = env;
1214
el = win.document.createElement('amp-story');
15+
win.document.body.appendChild(el);
1316
storeService = getStoreService(win);
14-
new StoryAnalyticsService(env.win, el);
17+
getAnalyticsService(win, el);
1518
});
1619

17-
it('sends story-page-visible on current page change', () => {
20+
it('sends story-page-visible on current page change', async () => {
1821
const triggerAnalyticsStub = env.sandbox.stub(
1922
analytics,
2023
'triggerAnalyticsEvent'
@@ -23,6 +26,9 @@ describes.realWin('amp-story-analytics', {amp: true}, (env) => {
2326
id: 'page-1',
2427
index: 0,
2528
});
29+
30+
await getAmpdoc(env.win.document).whenFirstVisible();
31+
2632
expect(triggerAnalyticsStub).to.have.been.calledOnceWithExactly(
2733
el,
2834
'story-page-visible',
@@ -43,7 +49,7 @@ describes.realWin('amp-story-analytics', {amp: true}, (env) => {
4349
expect(triggerAnalyticsStub).not.to.be.called;
4450
});
4551

46-
it('sends story-page-visible on content page after ad page', () => {
52+
it('does not send story-page-visible before document becomes visible', async () => {
4753
const triggerAnalyticsStub = env.sandbox.stub(
4854
analytics,
4955
'triggerAnalyticsEvent'
@@ -52,6 +58,29 @@ describes.realWin('amp-story-analytics', {amp: true}, (env) => {
5258
id: 'page-1',
5359
index: 0,
5460
});
61+
expect(triggerAnalyticsStub).not.to.be.called;
62+
63+
await getAmpdoc(env.win.document).whenFirstVisible();
64+
65+
expect(triggerAnalyticsStub).to.have.been.calledOnceWithExactly(
66+
el,
67+
'story-page-visible',
68+
env.sandbox.match({storyPageIndex: 0, storyPageId: 'page-1'})
69+
);
70+
});
71+
72+
it('sends story-page-visible on content page after ad page', async () => {
73+
const triggerAnalyticsStub = env.sandbox.stub(
74+
analytics,
75+
'triggerAnalyticsEvent'
76+
);
77+
storeService.dispatch(Action.CHANGE_PAGE, {
78+
id: 'page-1',
79+
index: 0,
80+
});
81+
82+
await getAmpdoc(env.win.document).whenFirstVisible();
83+
5584
expect(triggerAnalyticsStub).to.have.been.calledOnceWithExactly(
5685
el,
5786
'story-page-visible',
@@ -68,6 +97,9 @@ describes.realWin('amp-story-analytics', {amp: true}, (env) => {
6897
id: 'page-2',
6998
index: 2,
7099
});
100+
101+
await getAmpdoc(env.win.document).whenFirstVisible();
102+
71103
expect(triggerAnalyticsStub).to.have.been.calledTwice;
72104
expect(triggerAnalyticsStub.secondCall).to.have.been.calledWithExactly(
73105
el,

0 commit comments

Comments
 (0)