Previous page url value on SPA site
How can we get previous page url when site is single page application
document.referral is not working in this case
How can we get previous page url when site is single page application
document.referral is not working in this case
This is a fun one...
You're absolutely correct that document.referral will not work. Just for reference for anyone who doesn't know why, this is because an SPA only loads the physical page once (initially), so the referrer that brought you to the site will be maintained throughout the entire visit. So if you were referred via Google, google will be set as the referrer on every page (which is bad because this will inflate your Referrer Instances, showing every page as being a direct link from the site), and if the site is opened directly, there will be no referrer set which will inflate your typed/bookmarked referral instances.
Now, typically, since the developers are driving the update content and updating the browser history, I would generally ask the developers to populate my data layer with Referral information, then use that instead of the standard document.referrer.
However, if this isn't an option, you will have to come up with your own solution... in this case, what I would do is to use some custom code. Inside the set variables action on my tracking rule, I would check if my own custom "referrer" variable in the session storage (something like customReferrer).
On the first page load, my session storage variable wouldn't exist, and if it doesn't, then I would read document.referral and I would populate the s.referrer manually with the result.
Something like:
s.referrer = sessionStorage.getItem("customReferrer") ? sessionStorage.getItem("customReferrer") : document.referrer;
Since this is the first page load, the customReferrer won't yet exist, document.referrer will be returned, whether it has a referrer, or is a direct open (no referrer).
After the beacon has been sent, within the same rule, I would add a custom code to set customReferrer with the current URL (make sure that your actions are run in sequence - "Wait Then", not "Then").
sessionStorage.setItem("customReferrer",document.location.href)
On the next page when the page view is triggered, the customReferrer should have a value (your previous site page), so now this value should be used since it exists.
If the user reloads the page, you should still retain the session storage value and that value should be picked up by your code.
Obviously you would want to test this all thoroughly... or like I said at the beginning, get your developers to do all this logic into your data layer so all you have to do is read from there.
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.