From 4da658e72ad42cf251e4fb100ca651a7d4dca79e Mon Sep 17 00:00:00 2001 From: Simon Backx Date: Wed, 14 Jun 2023 15:22:59 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Reused=20attribution=20for=20emb?= =?UTF-8?q?eddable=20signup=20forms=20on=20own=20site?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fixes https://github.com/TryGhost/Team/issues/3473 When a signup happens via the embeddable signup form on your own site, we'll use the default attribution, so it indistinguishable from a signup via Portal. --- ghost/portal/src/utils/helpers.js | 1 + ghost/signup-form/src/utils/api.tsx | 2 +- ghost/signup-form/src/utils/helpers.tsx | 41 ++++++++++++++++++++++++- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/ghost/portal/src/utils/helpers.js b/ghost/portal/src/utils/helpers.js index f032f0144d..ebcf2baa60 100644 --- a/ghost/portal/src/utils/helpers.js +++ b/ghost/portal/src/utils/helpers.js @@ -869,6 +869,7 @@ export const transformApiTiersData = ({tiers}) => { /** * Returns the member attribution URL history, which is stored in localStorage, if there is any. + * @warning If you make changes here, please also update the one in signup-form! * @returns {Object[]|undefined} */ export function getUrlHistory() { diff --git a/ghost/signup-form/src/utils/api.tsx b/ghost/signup-form/src/utils/api.tsx index 5b0cc6c8cc..65dc51733f 100644 --- a/ghost/signup-form/src/utils/api.tsx +++ b/ghost/signup-form/src/utils/api.tsx @@ -19,7 +19,7 @@ export const setupGhostApi = ({siteUrl}: {siteUrl: string}) => { email, emailType: 'signup', labels, - urlHistory: getUrlHistory() + urlHistory: getUrlHistory({siteUrl}) }); const response = await fetch(url, { diff --git a/ghost/signup-form/src/utils/helpers.tsx b/ghost/signup-form/src/utils/helpers.tsx index a5863a6b35..84bd260a12 100644 --- a/ghost/signup-form/src/utils/helpers.tsx +++ b/ghost/signup-form/src/utils/helpers.tsx @@ -13,7 +13,46 @@ export function isMinimal(options: SignupFormOptions): boolean { return !options.title; } -export function getUrlHistory(): URLHistory { +/** + * Get the URL history when the form is embedded on the site itself. + */ +export function getDefaultUrlHistory() { + const STORAGE_KEY = 'ghost-history'; + + try { + const historyString = localStorage.getItem(STORAGE_KEY); + if (historyString) { + const parsed = JSON.parse(historyString); + + if (Array.isArray(parsed)) { + return parsed; + } + } + } catch (error) { + // Failed to access localStorage or something related to that. + // Log a warning, as this shouldn't happen on a modern browser. + + /* eslint-disable no-console */ + console.warn(`[Signup-Form] Failed to load member URL history:`, error); + } +} + +export function getUrlHistory({siteUrl}: {siteUrl: string}): URLHistory { + // If we are embedded on the site itself, use the default attribution localStorage, just like Portal + try { + if (window.location.host === new URL(siteUrl).host) { + const history = getDefaultUrlHistory(); + if (history) { + return history; + } + } + } catch (error) { + // Most likely an invalid siteUrl + + /* eslint-disable no-console */ + console.warn(`[Signup-Form] Failed to load member URL history:`, error); + } + const history: URLHistory = []; // Href without query string