Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:referrer #327

Merged
merged 12 commits into from
Jan 26, 2023
Merged

feat:referrer #327

merged 12 commits into from
Jan 26, 2023

Conversation

ps863
Copy link
Member

@ps863 ps863 commented Dec 12, 2022

Currently, AWS CloudWatch RUM does not support referrer. Information about the referrer for page views is not presently available.

This change adds the ability for the RUM web client to record and dispatch referrer information. The referrer will be stored in the referrer field in event details of a page view event. The referrer's domain will be stored in the referrerDomain field in event details of a page view event.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@ps863 ps863 changed the title feat:Referrer feat:referrer Dec 12, 2022
Comment on lines 135 to 137
document.referrer !== undefined && document.referrer !== ''
? document.referrer
: null,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: We can extract this check&set to a separate method since we're reusing it in 3 places.

Comment on lines 176 to 178
document.referrer !== undefined && document.referrer !== ''
? document.referrer
: null,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Use method mentioned in comment above

Comment on lines 189 to 191
document.referrer !== undefined && document.referrer !== ''
? document.referrer
: null,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Use method mentioned in comment above

Comment on lines 270 to 275
'([A-Za-z]+://[^/]+)[/]?'
);

if (domainName) {
return domainName[1];
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

questions:

  1. Is the purpose of the regex match to exclude trailing slashes (/)?
  2. For domains, we typically exclude thehttps:// prefix. Should we also exclude the prefix here as well?
  3. Do we not want to include localhost in the regex match?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Yes. However, will be using URL interface instead of regex for this. (See next revision)
  2. Excluded in next revision
  3. Next revision use of URL interface should handle localhost as well

@@ -487,3 +488,86 @@ describe('PageManager tests', () => {
expect(pageManager.getPage().start).toEqual(2500);
});
});

test('when complete referrer is available from the DOM', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: when complete referrer is available from the DOM --> when complete referrer is available from the DOM then is recorded in page view event

);
});

test('when only domain level referrer is available from the DOM', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: when only domain level referrer is available from the DOM --> when only domain level referrer is available from the DOM then is recorded in page view event

);
});

test('when referrer is not available from the DOM', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: when referrer is not available from the DOM --> when referrer from the DOM is not valid then it is not recorded in page view event

Comment on lines 259 to 269
if (document.referrer !== undefined && document.referrer !== '') {
const domainName = document.referrer.match(
'([A-Za-z]+://[^/]+)[/]?'
);

if (domainName) {
return domainName[1];
}
} else {
return null;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (document.referrer !== undefined && document.referrer !== '') {
const domainName = document.referrer.match(
'([A-Za-z]+://[^/]+)[/]?'
);
if (domainName) {
return domainName[1];
}
} else {
return null;
}
try {
return new URL(document.referrer).hostname;
} catch (e) {
return "";
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added in next revision

Comment on lines 272 to 281
/*
Get the referrer, if it can be read from the DOM
*/
private getReferrer() {
if (document.referrer !== undefined && document.referrer !== '') {
return document.referrer;
} else {
return null;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: Do we need this helper function? document.referrer should always be a URI or an empty string.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in next revision. The helper function is not needed

Comment on lines 232 to 238
if (page.referrer !== null) {
pageViewEvent.referrer = page.referrer;

if (page.referrerDomain !== null) {
pageViewEvent.referrerDomain = page.referrerDomain;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (page.referrer !== null) {
pageViewEvent.referrer = page.referrer;
if (page.referrerDomain !== null) {
pageViewEvent.referrerDomain = page.referrerDomain;
}
}
pageViewEvent.referrer = document.referrer;
pageViewEvent.referrerDomain = getDomainFromReferrer()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added in next revision

Comment on lines 575 to 578
expect(pageManager.getAttributes()).toMatchObject({
'aws:referrer': '',
'aws:referrerDomain': ''
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: do we want to record in metadata if it's empty? is this a conscience decision we're making?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://developer.mozilla.org/en-US/docs/Web/API/Document/referrer
States that -The value is an empty string if the user navigated to the page directly (not through a link, but, for example, by using a bookmark). , so we should record it

'popstate',
(pageManager as any).popstateListener
);
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: do we need a unit test for when localhost is the referrer?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handled in next revision

@@ -1,6 +1,7 @@
import { Selector } from 'testcafe';
import { REQUEST_BODY } from '../../test-utils/integ-test-utils';
import { PAGE_VIEW_EVENT_TYPE } from '../../plugins/utils/constant';
import { create } from 'lodash';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: unused

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed in next revision

Comment on lines 575 to 578
expect(pageManager.getAttributes()).toMatchObject({
'aws:referrer': '',
'aws:referrerDomain': ''
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://developer.mozilla.org/en-US/docs/Web/API/Document/referrer
States that -The value is an empty string if the user navigated to the page directly (not through a link, but, for example, by using a bookmark). , so we should record it

Comment on lines 262 to 266
if (document.referrer === 'localhost') {
// Handle special case for localhost
return 'localhost';
}
return '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (document.referrer === 'localhost') {
// Handle special case for localhost
return 'localhost';
}
return '';
return document.referrer === 'localhost' ? document.referrer : '';

Comment on lines +253 to +259
private getDomainFromReferrer() {
try {
return new URL(document.referrer).hostname;
} catch (e) {
return document.referrer === 'localhost' ? document.referrer : '';
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Consider covering these (localhost and empty referrer) with unit tests.

@ps863 ps863 merged commit a414c92 into aws-observability:main Jan 26, 2023
@ps863 ps863 mentioned this pull request Feb 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants