Skip to content

feat(analytics-browser): add page-url-previous-page plugin #1110

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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

cyhk
Copy link

@cyhk cyhk commented May 28, 2025

Summary

This adds a page-url-previous-page plugin to the analytics browser, enriching events with a set of page url and previous page related properties.

The PRD is here.

Note that the plugin is default off right now, but there are plans to make this default on in the future.

Checklist

  • Does your PR title have the correct title format?
  • Does your PR have a breaking change?: Yes

@cyhk cyhk changed the title AMP-130401 create previous page page url plugin feat(analytics-browser): add page-url-previous-page plugin May 28, 2025
@cyhk cyhk requested a review from Dogfalo June 2, 2025 17:37
@cyhk cyhk force-pushed the AMP-130401-create-previous-page-page-url-plugin branch 4 times, most recently from c2223a5 to d8e6eba Compare June 12, 2025 21:19
@cyhk cyhk marked this pull request as ready for review June 12, 2025 21:19
cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

@cyhk cyhk force-pushed the AMP-130401-create-previous-page-page-url-plugin branch from d5d2b40 to c3dbbce Compare June 13, 2025 04:21
cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

@cyhk
Copy link
Author

cyhk commented Jun 13, 2025

Bug: Invalid URL Handling Causes Function Failure

The getPrevPageType function lacks error handling for invalid URLs. The new URL(previousPage) constructor throws an error if previousPage (sourced from document.referrer or sessionStorage) is malformed or invalid, causing the function and potentially the plugin to crash.

packages/analytics-browser/src/plugins/page-url-previous-page.ts#L27-L40
Fix in Cursor

Was this report helpful? Give feedback by reacting with 👍 or 👎

this is fine since currentURL is never stored without first being run through getDecodeURI first and previousURL is always taken from currentURL

cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

@daniel-graham-amplitude
Copy link
Collaborator

Thanks @cyhk ! Generally looks, had some comments.

General question though: was there a formal process for deciding the namings of the config and events?

For the config (autocapture. pageUrlPreviousPage) and for the event properties ([Amplitude] Previous Page URL, etc...).

I'm asking just because API changes (in this case the config definition) and event properties can't be reverted once they've been released.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

✅ BugBot reviewed your changes and found no bugs!


Was this report helpful? Give feedback by reacting with 👍 or 👎

Copy link
Contributor

@Mercy811 Mercy811 left a comment

Choose a reason for hiding this comment

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

Thanks @cyhk for making this PR. I have two major concerns.

  1. Is it necessary to add more event properties to all events? Potential COGS. Or at least we should have a filter to decide which events to add.
  2. The definition of previous page url is the same as the event property referrer of page view. I read the proposal but didn't find the case where referrer cannot help.

let previousPage = '';
if (sessionStorage && isStorageEnabled) {
const URLInfo = await sessionStorage.get(URL_INFO_STORAGE_KEY);
previousPage = URLInfo?.[PREVIOUS_PAGE_STORAGE_KEY] || document.referrer || '';
Copy link
Contributor

Choose a reason for hiding this comment

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

May result in different previous page location and referrer event properties and thus cause confusion.

Comment on lines 144 to 157
event.event_properties = {
...(event.event_properties || {}),
'[Amplitude] Page Domain':
/* istanbul ignore next */ (typeof location !== 'undefined' && location.hostname) || '',
'[Amplitude] Page Location': locationHREF,
'[Amplitude] Page Path':
/* istanbul ignore next */ (typeof location !== 'undefined' && getDecodeURI(location.pathname)) || '',
'[Amplitude] Page Title':
/* istanbul ignore next */ (typeof document !== 'undefined' && document.title) || '',
'[Amplitude] Page URL': locationHREF.split('?')[0],
'[Amplitude] Previous Page Location': previousPage,
'[Amplitude] Previous Page Type': getPrevPageType(previousPage),
};
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Any COGS concern @izaaz if we add extra event properties to all events?

Any plan to add a filter to apply to specific events? I'm thinking about config.autocapture.pageUrlPreviousPage.trackOn, a function returning boolean.

@Dogfalo Dogfalo force-pushed the AMP-130401-create-previous-page-page-url-plugin branch from 6bf3702 to af0aa65 Compare June 30, 2025 16:54
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.

4 participants