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

fix: should default to empty string when document.title is not string #937

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -34,6 +34,25 @@ export const pageViewTrackingPlugin: CreatePageViewTrackingPlugin = (options: Op
return decodedLocationStr;
};

/* istanbul ignore next */
const getDocumentTitle = () => {
if (typeof document === 'undefined') {
localConfig?.loggerProvider.warn('document is undefined. Default [Amplitude] Page Title to an empty string');
return '';
}

const title = document.title;
if (typeof title !== 'string') {
localConfig?.loggerProvider.warn(
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`document.title is not a string. Default [Amplitude] Page Title to an empty string. document.title: ${title}`,
);
return '';
}

return title;
};

const createPageViewEvent = async (): Promise<Event> => {
/* istanbul ignore next */
const locationHREF = getDecodeURI((typeof location !== 'undefined' && location.href) || '');
Expand All @@ -46,7 +65,7 @@ export const pageViewTrackingPlugin: CreatePageViewTrackingPlugin = (options: Op
'[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 Title': getDocumentTitle(),
'[Amplitude] Page URL': locationHREF.split('?')[0],
},
};
Expand Down
Loading