{"id":92567,"date":"2025-02-17T00:09:47","date_gmt":"2025-02-17T00:09:47","guid":{"rendered":"https:\/\/peraltafinancing.com\/analytics\/persist-campaign-data-on-the-landing-page\/"},"modified":"2025-02-17T00:09:47","modified_gmt":"2025-02-17T00:09:47","slug":"persist-campaign-data-on-the-landing-page","status":"publish","type":"post","link":"https:\/\/fivemor.com\/?p=92567","title":{"rendered":"Persist Campaign Data On The Landing Page"},"content":{"rendered":"<p> <br \/>\n<\/p>\n<div>\n<p><a href=\"https:\/\/support.google.com\/analytics\/answer\/2790010?hl=en\">Universal Analytics<\/a> utilizes two components (by default) to attribute a browser session to a specific campaign: <strong>query parameters in the URL<\/strong> and the <strong>referrer string<\/strong>.<\/p>\n<p>The page URL is sent with every hit to Google Analytics using the <a href=\"https:\/\/developers.google.com\/analytics\/devguides\/collection\/analyticsjs\/field-reference#location\">Document location<\/a> field, which also translates to the <a href=\"https:\/\/developers.google.com\/analytics\/devguides\/collection\/protocol\/v1\/parameters#dl\"><code>&amp;dl<\/code><\/a> parameter in the <em>Measurement Protocol<\/em>.<\/p>\n<p>The referrer string is sent with a hit to Google Analytics using the <a href=\"https:\/\/developers.google.com\/analytics\/devguides\/collection\/analyticsjs\/field-reference#referrer\">Document referrer<\/a> field, as long as the referrer hostname does not exactly match that of the current page and the referrer string is not empty.<\/p>\n<p>When attributing a hit to a campaign session, Google Analytics first checks the <strong>location<\/strong> value for the following parameters:<\/p>\n<ul>\n<li><code>gclid<\/code> &#8211; for attributing the session to <a href=\"https:\/\/support.google.com\/analytics\/answer\/2938246?hl=en\">Google Ads<\/a><\/li>\n<li><code>utm_source<\/code> &#8211; for attributing the session to a custom campaign<\/li>\n<\/ul>\n<p>If these parameters are not found, Google Analytics looks at the <strong>referrer<\/strong> string.<\/p>\n<div style=\"aspect-ratio: 930 \/ 175;\" class=\"figure nocaption\">\n<p>    <a href=\"https:\/\/www.simoahava.com\/images\/2019\/12\/referrer-location.jpg\" title=\"location and referrer\"><\/p>\n<p>    <img decoding=\"async\" class=\"fig-img\" height=\"175\" width=\"930\" loading=\"lazy\" src=\"https:\/\/www.simoahava.com\/images\/2019\/12\/referrer-location.jpg#ZgotmplZ\" alt=\"location and referrer\"\/><\/p>\n<p>    <\/a><\/p>\n<\/div>\n<blockquote>\n<p>Yes, there are additional steps to campaign attribution (see <a href=\"https:\/\/support.google.com\/analytics\/answer\/6205762#flowchart\">the flowchart<\/a>), but these two are relevant in the context of this article.<\/p>\n<\/blockquote>\n<p>There are two contexts where attributing campaigns based on URLs and referrer strings becomes complicated.<\/p>\n<ol>\n<li>If the site <strong>blocks Google Analytics on the landing page<\/strong> (due to missing consent), but then makes it available on subsequent pages (due to granted consent), the original campaign URL and referrer string are no longer available.<\/li>\n<li>If the site is a <strong>single-page application<\/strong>, particularly <a href=\"https:\/\/tagmanager.google.com\/\">Google Tag Manager<\/a>-based tags become susceptible to the <a href=\"https:\/\/www.simoahava.com\/gtm-tips\/fix-rogue-referral-problem-single-page-sites\/\">rogue referral problem<\/a>.<\/li>\n<\/ol>\n<p>For this purpose, I have created a new <a href=\"https:\/\/www.simoahava.com\/analytics\/custom-templates-guide-for-google-tag-manager\/\">custom template<\/a>: <strong>Persist Campaign Data<\/strong>.<\/p>\n<div style=\"aspect-ratio: 499 \/ 403;\" class=\"figure nocaption\">\n<p>    <a href=\"https:\/\/www.simoahava.com\/images\/2019\/12\/persist-campaign-data.jpg\" title=\"Persist campaign data template\"><\/p>\n<p>    <img decoding=\"async\" class=\"fig-img\" height=\"403\" width=\"499\" loading=\"lazy\" src=\"https:\/\/www.simoahava.com\/images\/2019\/12\/persist-campaign-data.jpg#ZgotmplZ\" alt=\"Persist campaign data template\"\/><\/p>\n<p>    <\/a><\/p>\n<\/div>\n<p>This template tackles both of the problems listed above. However, you will need to make additional changes to your Google Analytics settings before the solution is complete.<\/p>\n<p>You can download the template from the <a href=\"https:\/\/tagmanager.google.com\/gallery\/#\/owners\/gtm-templates-simo-ahava\/templates\/persist-campaign-data\">template gallery<\/a>.<\/p>\n<p>                <span class=\"simmer\"><br \/>\n  <span class=\"close\">X<\/span><\/p>\n<p>\n    <span class=\"fa fa-md fa-bell\"\/><br \/>\n    <strong>The Simmer Newsletter<\/strong>\n  <\/p>\n<p>\n    Subscribe to the <a href=\"https:\/\/www.simoahava.com\/newsletter\/\">Simmer newsletter<\/a> to get the latest news and content from Simo Ahava into your email inbox!\n  <\/p>\n<p>  <\/span><\/p>\n<h2 id=\"1-store-campaign-data-in-a-session-cookie\">1. Store campaign data in a session cookie<\/h2>\n<p>The first problem is particularly bothersome on sites that utilize <strong>consent management systems<\/strong> to block Google Analytics if consent has not been granted.<\/p>\n<p>If consent is granted on the landing page, then there should be no issues. The page is reloaded with the campaign parameters in the URL and the referral string intact, and Google Analytics tags can make use of these when attributing the session to a specific campaign.<\/p>\n<p>However, if the user grants consent only <strong>after navigating deeper into the site<\/strong>, the tags that fire on these pages will no longer have access to the initial campaign that brought the user to the site, which leads to <strong>inflation of direct traffic<\/strong>.<\/p>\n<p>To solve this problem, the template tag (assuming Google Tag Manager is not blocked, too) writes the URL of the current page in a cookie <em>if<\/em> it has campaign-setting parameters. Similarly, the referrer string is also written in a cookie <em>if<\/em> its hostname does not contain the current page hostname.<\/p>\n<div style=\"aspect-ratio: 995 \/ 59;\" class=\"figure nocaption\">\n<p>    <a href=\"https:\/\/www.simoahava.com\/images\/2019\/12\/campaign-cookies.jpg\" title=\"Campaign cookies\"><\/p>\n<p>    <img decoding=\"async\" class=\"fig-img\" height=\"59\" width=\"995\" loading=\"lazy\" src=\"https:\/\/www.simoahava.com\/images\/2019\/12\/campaign-cookies.jpg#ZgotmplZ\" alt=\"Campaign cookies\"\/><\/p>\n<p>    <\/a><\/p>\n<\/div>\n<p>Then, when the Google Analytics tags finally fire, they can take the original location and referrer from these cookies to attribute the session to its correct source.<\/p>\n<h3 id=\"setup-the-persist-campaign-data-tag\">Setup the Persist Campaign Data tag<\/h3>\n<p>Create a new tag with the <strong>Persist Campaign Data<\/strong> template. Check the box next to <strong>Store campaign data in a browser cookie<\/strong> and check that the settings are OK.<\/p>\n<div style=\"aspect-ratio: 558 \/ 384;\" class=\"figure nocaption\">\n<p>    <a href=\"https:\/\/www.simoahava.com\/images\/2019\/12\/store-campaign-cookie.jpg\" title=\"Store campaign data in a browser cookie\"><\/p>\n<p>    <img decoding=\"async\" class=\"fig-img\" height=\"384\" width=\"558\" loading=\"lazy\" src=\"https:\/\/www.simoahava.com\/images\/2019\/12\/store-campaign-cookie.jpg#ZgotmplZ\" alt=\"Store campaign data in a browser cookie\"\/><\/p>\n<p>    <\/a><\/p>\n<\/div>\n<p>By default, the following URL parameters will cause the tag to set the campaign URL cookie:<\/p>\n<ul>\n<li><code>utm_source<\/code><\/li>\n<li><code>utm_medium<\/code><\/li>\n<li><code>utm_campaign<\/code><\/li>\n<li><code>utm_term<\/code><\/li>\n<li><code>utm_content<\/code><\/li>\n<li><code>utm_id<\/code><\/li>\n<li><code>gclid<\/code><\/li>\n<\/ul>\n<p>Naturally, you can add additional parameters to the list if you wish.<\/p>\n<p>Not all of these parameters have the ability to start a new session (only <code>utm_source<\/code> and <code>gclid<\/code> do, in fact), but the assumption is that if you\u2019ve set any UTM parameters, you want to include them with your hits.<\/p>\n<p>Next, you can change the default name of the URL cookie and the referrer cookie, if you wish.<\/p>\n<blockquote>\n<p><strong>NOTE!<\/strong> If you do change the name of either cookie, you <strong>must<\/strong> edit the <a href=\"https:\/\/www.simoahava.com\/analytics\/custom-templates-guide-for-google-tag-manager\/#sets-a-cookie-value\">template permissions<\/a> and allow the template to set the new cookie name(s).<\/p>\n<\/blockquote>\n<p>The tag will only write the cookies in specific conditions:<\/p>\n<ol>\n<li>The <strong>URL<\/strong> cookie is only written if the URL query string has any one of the listed trigger parameters. If such a parameter is found, the full URL is written into the campaign URL cookie.<\/li>\n<li>The <strong>Referrer<\/strong> cookie is only written if the referrer hostname does not contain the current page hostname, and the referrer string is not empty. If both conditions are good, the full referrer string is written into the referrer cookie.<\/li>\n<\/ol>\n<p>Thus it\u2019s safe to set it on the <strong>All Pages<\/strong> trigger, as it doesn\u2019t matter if it fires on every single page.<\/p>\n<p><strong>Note!<\/strong> You might want to actually set it on a trigger that fires once user consent has been established &#8211; if the user gives consent or has already given consent, the tag does not need to (and should not) fire, because then your Google Analytics tags would fire normally and your wouldn\u2019t need to save the URL and referrer data in cookies.<\/p>\n<h3 id=\"create-the-cookie-variables\">Create the cookie variables<\/h3>\n<p>You\u2019ll need two new, user-defined <strong>1st Party Cookie<\/strong> variables.<\/p>\n<p>The first, <strong>{{Cookie &#8211; __gtm_campaign_url}}<\/strong>. The second, <strong>{{Cookie &#8211; __gtm_referrer}}<\/strong>.<\/p>\n<p>Make sure you check the <strong>URI-decode cookie<\/strong> box.<\/p>\n<div style=\"aspect-ratio: 476 \/ 360;\" class=\"figure nocaption\">\n<p>    <a href=\"https:\/\/www.simoahava.com\/images\/2019\/12\/campaign-url-cookie.jpg\" title=\"Campaign URL cookie\"><\/p>\n<p>    <img decoding=\"async\" class=\"fig-img\" height=\"360\" width=\"476\" loading=\"lazy\" src=\"https:\/\/www.simoahava.com\/images\/2019\/12\/campaign-url-cookie.jpg#ZgotmplZ\" alt=\"Campaign URL cookie\"\/><\/p>\n<p>    <\/a><\/p>\n<\/div>\n<div style=\"aspect-ratio: 424 \/ 359;\" class=\"figure nocaption\">\n<p>    <a href=\"https:\/\/www.simoahava.com\/images\/2019\/12\/referrer-cookie.jpg\" title=\"referrer-cookie.jpg\"><\/p>\n<p>    <img decoding=\"async\" class=\"fig-img\" height=\"359\" width=\"424\" loading=\"lazy\" src=\"https:\/\/www.simoahava.com\/images\/2019\/12\/referrer-cookie.jpg#ZgotmplZ\" alt=\"referrer-cookie.jpg\"\/><\/p>\n<p>    <\/a><\/p>\n<\/div>\n<h3 id=\"create-the-customtask\">Create the <code>customTask<\/code><\/h3>\n<p>To update your Google Analytics tags, we\u2019ll be using a <a href=\"https:\/\/www.simoahava.com\/analytics\/customtask-the-guide\/\"><code>customTask<\/code><\/a>. This is an easy way to make sure the cookies are only used if they exist, and it\u2019s a great way to actually delete the cookies as soon as they\u2019ve been used, so that they don\u2019t persist needlessly.<\/p>\n<blockquote>\n<p><strong>NOTE!<\/strong> You don\u2019t <em>have<\/em> to use a <code>customTask<\/code>. You can set the <code>location<\/code>, <code>page<\/code>, and <code>referrer<\/code> fields manually on the tag, if you wish. You just need to add proper fallback values if the cookies do not exist, otherwise you risk corrupting your data with invalid <code>location<\/code> and <code>referrer<\/code> fields.<\/p>\n<\/blockquote>\n<p>If you need the cookies for something else as well, then you can set the <code>customTask<\/code> to <strong>not<\/strong> delete cookies after the values have been \u201cused\u201d.<\/p>\n<p>Create a new <strong>Custom JavaScript variable<\/strong>, name it <strong>{{customTask &#8211; set location and referrer from cookie}}<\/strong>, and add the following code within:<\/p>\n<div class=\"highlight\">\n<pre style=\"background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4\"><code class=\"language-javascript\" data-lang=\"javascript\"><span style=\"color:#00a\">function<\/span>() {\n  <span style=\"color:#00a\">return<\/span> <span style=\"color:#00a\">function<\/span>(customModel) {\n    <span style=\"color:#aaa;font-style:italic\">\/\/ Set to the cookie variables\n<\/span><span style=\"color:#aaa;font-style:italic\"\/>    <span style=\"color:#00a\">var<\/span> storedLocation = {{Cookie - __gtm_campaign_url}};\n    <span style=\"color:#00a\">var<\/span> storedReferrer = {{Cookie - __gtm_referrer}};\n    <span style=\"color:#aaa;font-style:italic\">\/\/ Set to the cookie names\n<\/span><span style=\"color:#aaa;font-style:italic\"\/>    <span style=\"color:#00a\">var<\/span> storedLocationCookieName = <span style=\"color:#a50\">'__gtm_campaign_url'<\/span>;\n    <span style=\"color:#00a\">var<\/span> storedReferrerCookieName = <span style=\"color:#a50\">'__gtm_referrer'<\/span>;\n    <span style=\"color:#aaa;font-style:italic\">\/\/ Set to the root domain of your site (e.g. domain name without any subdomain, \"mysite.com\")\n<\/span><span style=\"color:#aaa;font-style:italic\"\/>    <span style=\"color:#00a\">var<\/span> domainName = <span style=\"color:#a50\">'simoahava.com'<\/span>;\n    <span style=\"color:#aaa;font-style:italic\">\/\/ Choose whether to delete a cookie after its value is used (true\/false)\n<\/span><span style=\"color:#aaa;font-style:italic\"\/>    <span style=\"color:#00a\">var<\/span> deleteCookieToggle = <span style=\"color:#00a\">true<\/span>;\n\n    <span style=\"color:#00a\">var<\/span> deleteCookie = <span style=\"color:#00a\">function<\/span>(name) {\n      <span style=\"color:#0aa\">document<\/span>.cookie = name + <span style=\"color:#a50\">'=; path=\/; domain='<\/span> + domainName + <span style=\"color:#a50\">'; expires=Thu, 01 Jan 1970 00:00:00 GMT'<\/span>;\n    };\n    \n    <span style=\"color:#00a\">if<\/span> (<span style=\"color:#00a\">typeof<\/span> storedLocation !== <span style=\"color:#a50\">'undefined'<\/span>) {\n      customModel.set(<span style=\"color:#a50\">'location'<\/span>, storedLocation);\n      customModel.set(<span style=\"color:#a50\">'page'<\/span>, <span style=\"color:#0aa\">document<\/span>.location.pathname + <span style=\"color:#0aa\">document<\/span>.location.search);\n      <span style=\"color:#00a\">if<\/span> (deleteCookieToggle === <span style=\"color:#00a\">true<\/span>) { deleteCookie(storedLocationCookieName); }\n    }\n    <span style=\"color:#00a\">if<\/span> (<span style=\"color:#00a\">typeof<\/span> storedReferrer !== <span style=\"color:#a50\">'undefined'<\/span>) {\n      customModel.set(<span style=\"color:#a50\">'referrer'<\/span>, storedReferrer);\n      <span style=\"color:#00a\">if<\/span> (deleteCookieToggle === <span style=\"color:#00a\">true<\/span>) { deleteCookie(storedReferrerCookieName); }\n    }\n  };\n}\n<\/code><\/pre>\n<\/div>\n<p>There are some things you might need to edit.<\/p>\n<p>Change the <code>storedLocation<\/code> and <code>storedReferrer<\/code> variables to point to the correct Google Tag Manager 1st Party Cookie variables you\u2019ve created, if you\u2019ve diverted from the default values used in this guide.<\/p>\n<p>Change the <code>storedLocationCookieName<\/code> and <code>storedReferrerCookieName<\/code> to the correct cookie names, if you\u2019ve diverted from the default values used in this guide.<\/p>\n<p>Set the <code>domainName<\/code> to the correct root domain (domain name without any subdomains included).<\/p>\n<p>Set the <code>deleteCookieToggle<\/code> to <code>false<\/code> if you don\u2019t want the task to remove cookies once their values have been \u201cused\u201d.<\/p>\n<p>This <code>customTask<\/code> sets the <code>location<\/code> and<code>referrer<\/code> fields if the respective cookies exist, and then (optionally) deletes the cookies for those fields that were set. If a cookie doesn\u2019t exist, its field is not set.<\/p>\n<p>If the <code>location<\/code> is set, the <code>customTask<\/code> also sets the <code>page<\/code> field to the current page path and query string. If it didn\u2019t do this, then the tag would take the page path from the <code>location<\/code> as well, which would inflate the pageviews of the landing page rather than increment them for the page the user is actually on.<\/p>\n<h3 id=\"add-the-customtask-to-your-google-analytics-tags\">Add the <code>customTask<\/code> to your Google Analytics tags<\/h3>\n<p>Finally, add the <code>customTask<\/code> variable you just created to your Google Analytics tags. You only need to have it fire on the first tag that fires on the page, but just to be safe it might make sense to add it to your <a href=\"https:\/\/www.simoahava.com\/analytics\/google-analytics-settings-variable-in-gtm\/\">Google Analytics Settings variable<\/a>, and then attach that to all your tags.<\/p>\n<div style=\"aspect-ratio: 892 \/ 451;\" class=\"figure nocaption\">\n<p>    <a href=\"https:\/\/www.simoahava.com\/images\/2019\/12\/gas-variable.jpg\" title=\"Google Analytics Settings variable\"><\/p>\n<p>    <img decoding=\"async\" class=\"fig-img\" height=\"451\" width=\"892\" loading=\"lazy\" src=\"https:\/\/www.simoahava.com\/images\/2019\/12\/gas-variable.jpg#ZgotmplZ\" alt=\"Google Analytics Settings variable\"\/><\/p>\n<p>    <\/a><\/p>\n<\/div>\n<p>The field name is <strong>customTask<\/strong> and the value is <strong>{{customTask &#8211; set location and referrer from cookie}}<\/strong>.<\/p>\n<h3 id=\"store-campaign-data---summary\">Store campaign data &#8211; summary<\/h3>\n<p>If the user lands on your site with campaign parameters in the URL and\/or a referrer that is external to your website\u2019s domain, the following happens:<\/p>\n<ol>\n<li>The <strong>Persist Campaign Data<\/strong> tag creates new cookies where necessary; one for the URL of the page (if it had campaign parameters) and one for the referrer string (if it\u2019s an external domain).<\/li>\n<li>Once the user gives consent, your Google Analytics tags utilize a <code>customTask<\/code> which <strong>sets the <code>location<\/code> and <code>referrer<\/code> fields<\/strong> from the cookies, and subsequently deletes the cookies.<\/li>\n<li>Thus the campaign URL and the referrer string are preserved for Google Analytics tags that might not fire until later in the site navigation, when the original URL parameters and referrer string are just a memory.<\/li>\n<\/ol>\n<p>There are some caveats to note.<\/p>\n<ol>\n<li>If you already have a <code>customTask<\/code> set, you need to <strong>modify<\/strong> it to include the <code>customTask<\/code> code from this guide. See <a href=\"https:\/\/www.simoahava.com\/analytics\/customtask-the-guide\/#putting-it-all-together-literally\">this<\/a> for inspiration.<\/li>\n<li>There might be a <strong>race condition<\/strong> where the Persist Campaign Data tag completes <em>after<\/em> the Google Analytics tag has fired, leading to cookies being created but not utilized (and subsequently deleted). An easy way to avoid this problem is to make sure the Persist Campaign Data tag only fires if user has <strong>not<\/strong> given consent. That way it will only fire if the Google Analytics have been blocked.<\/li>\n<li>The cookies are <strong>session cookies<\/strong> so if the user closes the browser and then re-opens it to continue navigation, the stored data is lost.<\/li>\n<li><strong>Cross-domain tracking<\/strong> is something that is very likely lost if you delay firing your tags beyond the landing page &#8211; this solution will not help with this problem. You could take the Client ID from the linker parameter, but since you\u2019re not validating the linker parameter, the data would be vulnerable to link sharing (and any users who follow the shared link would be counted as the same user by Google Analytics).<\/li>\n<\/ol>\n<h2 id=\"2-push-original-location-in-datalayer\">2. Push original location in dataLayer<\/h2>\n<p>The <a href=\"https:\/\/www.simoahava.com\/gtm-tips\/fix-rogue-referral-problem-single-page-sites\/\">rogue referral problem<\/a> is a scenario where tags firing on a single-page app use the \u201cvirtual\u201d location for campaign attribution (often missing the original campaign parameters) and thus Google Analytics reverts to the referrer instead. This results in sessions where hits sent on the first page are part of a <code>google \/ cpc<\/code> session, but as soon as the user navigates to other parts of the SPA, the campaign changes to the referral source, such as <code>google \/ organic<\/code>.<\/p>\n<div style=\"aspect-ratio: 709 \/ 236;\" class=\"figure nocaption\">\n<p>    <a href=\"https:\/\/www.simoahava.com\/images\/2019\/12\/rogue-referral.jpg\" title=\"Rogue referrer\"><\/p>\n<p>    <img decoding=\"async\" class=\"fig-img\" height=\"236\" width=\"709\" loading=\"lazy\" src=\"https:\/\/www.simoahava.com\/images\/2019\/12\/rogue-referral.jpg#ZgotmplZ\" alt=\"Rogue referrer\"\/><\/p>\n<p>    <\/a><\/p>\n<\/div>\n<blockquote>\n<p><strong>NOTE!<\/strong> Please understand that these steps must only be taken if the site is a <strong>single-page app<\/strong> or has single-page app components. There\u2019s no harm in setting this up on a regular site, but it\u2019s just extra work and clutter in the container.<\/p>\n<\/blockquote>\n<p>To fix this, you need the following.<\/p>\n<h3 id=\"setup-the-persist-campaign-data-tag-1\">Setup the Persist Campaign Data tag<\/h3>\n<p>Create a new <strong>tag<\/strong> with the <strong>Persist Campaign Data<\/strong> template. Check the box next to <strong>Push original location in dataLayer<\/strong> and check that the settings are OK.<\/p>\n<div style=\"aspect-ratio: 906 \/ 567;\" class=\"figure nocaption\">\n<p>    <a href=\"https:\/\/www.simoahava.com\/images\/2019\/12\/push-original-location.jpg\" title=\"Push original location in dataLayer\"><\/p>\n<p>    <img decoding=\"async\" class=\"fig-img\" height=\"567\" width=\"906\" loading=\"lazy\" src=\"https:\/\/www.simoahava.com\/images\/2019\/12\/push-original-location.jpg#ZgotmplZ\" alt=\"Push original location in dataLayer\"\/><\/p>\n<p>    <\/a><\/p>\n<\/div>\n<p>Set this tag to fire on the <strong>All Pages<\/strong> trigger.<\/p>\n<p>This tag, when fires, does a very simple thing. It writes the current URL into the <code>dataLayer<\/code> using the key <code>originalLocation<\/code>.<\/p>\n<p>It\u2019s important that this tag only fires <strong>once<\/strong> on the initial page load (hence the All Pages trigger).<\/p>\n<h3 id=\"create-the-data-layer-variable\">Create the Data Layer Variable<\/h3>\n<p>Next, create a Data Layer variable named <strong>{{Original Location}}<\/strong>, and set it to point to the <code>originalLocation<\/code> key. Importantly, set its <strong>Default Value<\/strong> to {{Page URL}}.<\/p>\n<div style=\"aspect-ratio: 594 \/ 513;\" class=\"figure nocaption\">\n<p>    <a href=\"https:\/\/www.simoahava.com\/images\/2019\/12\/original-location.jpg\" title=\"Original Location\"><\/p>\n<p>    <img decoding=\"async\" class=\"fig-img\" height=\"513\" width=\"594\" loading=\"lazy\" src=\"https:\/\/www.simoahava.com\/images\/2019\/12\/original-location.jpg#ZgotmplZ\" alt=\"Original Location\"\/><\/p>\n<p>    <\/a><\/p>\n<\/div>\n<blockquote>\n<p>The <strong>Default Value<\/strong> is very important. It\u2019s more than likely that a Google Analytics tag fires before <code>originalLocation<\/code> is pushed into <code>dataLayer<\/code>, so the variable simply returns the current page URL (which should be the same as <code>originalLocation<\/code>) in case such a race condition emerges.<\/p>\n<\/blockquote>\n<h3 id=\"update-your-google-analytics-tags\">Update your Google Analytics tags<\/h3>\n<p>Now that you have the template tag firing on the All Pages trigger, and you have the Data Layer variable ready, you need to edit <strong>every single one of your Google Analytics tags<\/strong>. Easiest way to do this is with the <a href=\"https:\/\/www.simoahava.com\/analytics\/google-analytics-settings-variable-in-gtm\/\">Google Analytics Settings variable<\/a>.<\/p>\n<blockquote>\n<p><strong>NOTE!<\/strong> It\u2019s vital that <em>every single Google Analytics tag<\/em> has this modification. If there\u2019s even a single tag that fires without the fixed <em>location<\/em>, it\u2019s possible that you campaign data will be broken.<\/p>\n<\/blockquote>\n<p>You need to add <a href=\"https:\/\/www.simoahava.com\/analytics\/edit-google-analytics-tag-settings\/\">two fields<\/a> to the variable:<\/p>\n<p><strong>Field name<\/strong>: <code>location<\/code><br \/><strong>Value<\/strong>: <code>{{Original Location}}<\/code><\/p>\n<p><strong>Field name<\/strong>: <code>page<\/code><br \/><strong>Value<\/strong>: <code>{{Page Path}}<\/code><\/p>\n<p>The value of <code>page<\/code> should be whatever you currently use for your virtual pageviews on your single-page app. It could be a Data Layer variable, a history state variable, or it could simply be the current page URL or current page path, as in the example above.<\/p>\n<p>Both of these fields are required to be set in your tags.<\/p>\n<h3 id=\"how-it-works\">How it works<\/h3>\n<p>Once you\u2019ve set it up, here\u2019s what happens.<\/p>\n<ol>\n<li>When the user lands on the page, the page URL is stored in the <code>originalLocation<\/code> Data Layer variable.<\/li>\n<li>When a Google Analytics tag fires, it takes the <em>location<\/em> field from this variable. This is because the <em>location<\/em> field is used for campaign attribution, and the landing page has all these parameters.<\/li>\n<li>The <em>page<\/em> field is set so that a virtual page path could be set for your single-page app page views.<\/li>\n<\/ol>\n<p>Thus by always sending the URL of the initial page load as the value of the <em>location<\/em> field, the <a href=\"https:\/\/www.simoahava.com\/gtm-tips\/fix-rogue-referral-problem-single-page-sites\/\">rogue referral problem<\/a> is fixed.<\/p>\n<h2 id=\"summary\">Summary<\/h2>\n<p>The <a href=\"https:\/\/tagmanager.google.com\/gallery\/#\/owners\/gtm-templates-simo-ahava\/templates\/persist-campaign-data\">Persist Campaign Data<\/a> template addresses two problems with campaign tracking in Google Analytics.<\/p>\n<ol>\n<li>Delayed \/ deferred Google Analytics tracking, where the first hits of the session are sent <strong>after<\/strong> the user has navigated away from the landing page.<\/li>\n<li>Single-page application tracking, where campaign sessions get incorrectly attributed to the referrer string rather than the parameters in the URL.<\/li>\n<\/ol>\n<p>You can, of course, utilize the features for other things as well. Persisting campaign parameters can have uses beyond Google Analytics tracking, and identifying the URL of the first page load of a SPA could similarly be used to understand navigation patterns better.<\/p>\n<p>As always, I look forward to hearing from you in the comments. Do you see problems with either approach? Do you have suggestions for improving the template or the implementation thereof?<\/p>\n<\/p><\/div>\n\n","protected":false},"excerpt":{"rendered":"<p>Universal Analytics utilizes two components (by default) to attribute a browser session to a specific campaign: query parameters in the URL and the referrer string. The page URL is sent with every hit to Google Analytics using the Document location field, which also translates to the &amp;dl parameter in the Measurement Protocol. The referrer string [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":92568,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12033],"tags":[13503,11603,3347,4347,21084],"dealstore":[],"offerexpiration":[],"class_list":["post-92567","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-analytics","tag-campaign","tag-data","tag-landing","tag-page","tag-persist"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Persist Campaign Data On The Landing Page - Som2ny Network<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/fivemor.com\/?p=92567\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Persist Campaign Data On The Landing Page - Som2ny Network\" \/>\n<meta property=\"og:description\" content=\"Universal Analytics utilizes two components (by default) to attribute a browser session to a specific campaign: query parameters in the URL and the referrer string. The page URL is sent with every hit to Google Analytics using the Document location field, which also translates to the &amp;dl parameter in the Measurement Protocol. The referrer string [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/fivemor.com\/?p=92567\" \/>\n<meta property=\"og:site_name\" content=\"Som2ny Network\" \/>\n<meta property=\"article:published_time\" content=\"2025-02-17T00:09:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/fivemor.com\/wp-content\/uploads\/2025\/02\/referrer-location.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"930\" \/>\n\t<meta property=\"og:image:height\" content=\"175\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/fivemor.com\/?p=92567#article\",\"isPartOf\":{\"@id\":\"https:\/\/fivemor.com\/?p=92567\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\/\/fivemor.com\/#\/schema\/person\/b85e3c3dc0e1daea076524dc8810c371\"},\"headline\":\"Persist Campaign Data On The Landing Page\",\"datePublished\":\"2025-02-17T00:09:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/fivemor.com\/?p=92567\"},\"wordCount\":2270,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/fivemor.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/fivemor.com\/?p=92567#primaryimage\"},\"thumbnailUrl\":\"https:\/\/fivemor.com\/wp-content\/uploads\/2025\/02\/referrer-location.jpg\",\"keywords\":[\"Campaign\",\"Data\",\"Landing\",\"Page\",\"persist\"],\"articleSection\":[\"Analytics\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/fivemor.com\/?p=92567#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/fivemor.com\/?p=92567\",\"url\":\"https:\/\/fivemor.com\/?p=92567\",\"name\":\"Persist Campaign Data On The Landing Page - Som2ny Network\",\"isPartOf\":{\"@id\":\"https:\/\/fivemor.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/fivemor.com\/?p=92567#primaryimage\"},\"image\":{\"@id\":\"https:\/\/fivemor.com\/?p=92567#primaryimage\"},\"thumbnailUrl\":\"https:\/\/fivemor.com\/wp-content\/uploads\/2025\/02\/referrer-location.jpg\",\"datePublished\":\"2025-02-17T00:09:47+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/fivemor.com\/?p=92567#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/fivemor.com\/?p=92567\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/fivemor.com\/?p=92567#primaryimage\",\"url\":\"https:\/\/fivemor.com\/wp-content\/uploads\/2025\/02\/referrer-location.jpg\",\"contentUrl\":\"https:\/\/fivemor.com\/wp-content\/uploads\/2025\/02\/referrer-location.jpg\",\"width\":930,\"height\":175},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/fivemor.com\/?p=92567#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/fivemor.com\/?bp_activities=1\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Persist Campaign Data On The Landing Page\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/fivemor.com\/#website\",\"url\":\"https:\/\/fivemor.com\/\",\"name\":\"Som2ny Network\",\"description\":\"Daily Deals\",\"publisher\":{\"@id\":\"https:\/\/fivemor.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/fivemor.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/fivemor.com\/#organization\",\"name\":\"Som2ny Network\",\"url\":\"https:\/\/fivemor.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/fivemor.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/fivemor.com\/wp-content\/uploads\/2026\/07\/4a0953c4-logo-300x86-1.png\",\"contentUrl\":\"https:\/\/fivemor.com\/wp-content\/uploads\/2026\/07\/4a0953c4-logo-300x86-1.png\",\"width\":300,\"height\":86,\"caption\":\"Som2ny Network\"},\"image\":{\"@id\":\"https:\/\/fivemor.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/fivemor.com\/#\/schema\/person\/b85e3c3dc0e1daea076524dc8810c371\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/fivemor.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/729ae85bf62b9917e93538db2f2688ca?s=96&r=g&default=https%3A%2F%2Ffivemor.com%2Fwp-content%2Fplugins%2Fbuddypress-first-letter-avatar%2Fimages%2Fdefault%2F96%2Flatin_a.png\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/729ae85bf62b9917e93538db2f2688ca?s=96&r=g&default=https%3A%2F%2Ffivemor.com%2Fwp-content%2Fplugins%2Fbuddypress-first-letter-avatar%2Fimages%2Fdefault%2F96%2Flatin_a.png\",\"caption\":\"admin\"},\"sameAs\":[\"https:\/\/fivemor.com\"],\"url\":\"https:\/\/fivemor.com\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Persist Campaign Data On The Landing Page - Som2ny Network","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/fivemor.com\/?p=92567","og_locale":"en_US","og_type":"article","og_title":"Persist Campaign Data On The Landing Page - Som2ny Network","og_description":"Universal Analytics utilizes two components (by default) to attribute a browser session to a specific campaign: query parameters in the URL and the referrer string. The page URL is sent with every hit to Google Analytics using the Document location field, which also translates to the &amp;dl parameter in the Measurement Protocol. The referrer string [&hellip;]","og_url":"https:\/\/fivemor.com\/?p=92567","og_site_name":"Som2ny Network","article_published_time":"2025-02-17T00:09:47+00:00","og_image":[{"width":930,"height":175,"url":"https:\/\/fivemor.com\/wp-content\/uploads\/2025\/02\/referrer-location.jpg","type":"image\/jpeg"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/fivemor.com\/?p=92567#article","isPartOf":{"@id":"https:\/\/fivemor.com\/?p=92567"},"author":{"name":"admin","@id":"https:\/\/fivemor.com\/#\/schema\/person\/b85e3c3dc0e1daea076524dc8810c371"},"headline":"Persist Campaign Data On The Landing Page","datePublished":"2025-02-17T00:09:47+00:00","mainEntityOfPage":{"@id":"https:\/\/fivemor.com\/?p=92567"},"wordCount":2270,"commentCount":0,"publisher":{"@id":"https:\/\/fivemor.com\/#organization"},"image":{"@id":"https:\/\/fivemor.com\/?p=92567#primaryimage"},"thumbnailUrl":"https:\/\/fivemor.com\/wp-content\/uploads\/2025\/02\/referrer-location.jpg","keywords":["Campaign","Data","Landing","Page","persist"],"articleSection":["Analytics"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/fivemor.com\/?p=92567#respond"]}]},{"@type":"WebPage","@id":"https:\/\/fivemor.com\/?p=92567","url":"https:\/\/fivemor.com\/?p=92567","name":"Persist Campaign Data On The Landing Page - Som2ny Network","isPartOf":{"@id":"https:\/\/fivemor.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/fivemor.com\/?p=92567#primaryimage"},"image":{"@id":"https:\/\/fivemor.com\/?p=92567#primaryimage"},"thumbnailUrl":"https:\/\/fivemor.com\/wp-content\/uploads\/2025\/02\/referrer-location.jpg","datePublished":"2025-02-17T00:09:47+00:00","breadcrumb":{"@id":"https:\/\/fivemor.com\/?p=92567#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/fivemor.com\/?p=92567"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/fivemor.com\/?p=92567#primaryimage","url":"https:\/\/fivemor.com\/wp-content\/uploads\/2025\/02\/referrer-location.jpg","contentUrl":"https:\/\/fivemor.com\/wp-content\/uploads\/2025\/02\/referrer-location.jpg","width":930,"height":175},{"@type":"BreadcrumbList","@id":"https:\/\/fivemor.com\/?p=92567#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/fivemor.com\/?bp_activities=1"},{"@type":"ListItem","position":2,"name":"Persist Campaign Data On The Landing Page"}]},{"@type":"WebSite","@id":"https:\/\/fivemor.com\/#website","url":"https:\/\/fivemor.com\/","name":"Som2ny Network","description":"Daily Deals","publisher":{"@id":"https:\/\/fivemor.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/fivemor.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/fivemor.com\/#organization","name":"Som2ny Network","url":"https:\/\/fivemor.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/fivemor.com\/#\/schema\/logo\/image\/","url":"https:\/\/fivemor.com\/wp-content\/uploads\/2026\/07\/4a0953c4-logo-300x86-1.png","contentUrl":"https:\/\/fivemor.com\/wp-content\/uploads\/2026\/07\/4a0953c4-logo-300x86-1.png","width":300,"height":86,"caption":"Som2ny Network"},"image":{"@id":"https:\/\/fivemor.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/fivemor.com\/#\/schema\/person\/b85e3c3dc0e1daea076524dc8810c371","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/fivemor.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/729ae85bf62b9917e93538db2f2688ca?s=96&r=g&default=https%3A%2F%2Ffivemor.com%2Fwp-content%2Fplugins%2Fbuddypress-first-letter-avatar%2Fimages%2Fdefault%2F96%2Flatin_a.png","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/729ae85bf62b9917e93538db2f2688ca?s=96&r=g&default=https%3A%2F%2Ffivemor.com%2Fwp-content%2Fplugins%2Fbuddypress-first-letter-avatar%2Fimages%2Fdefault%2F96%2Flatin_a.png","caption":"admin"},"sameAs":["https:\/\/fivemor.com"],"url":"https:\/\/fivemor.com\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/fivemor.com\/index.php?rest_route=\/wp\/v2\/posts\/92567","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fivemor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fivemor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fivemor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fivemor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=92567"}],"version-history":[{"count":0,"href":"https:\/\/fivemor.com\/index.php?rest_route=\/wp\/v2\/posts\/92567\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/fivemor.com\/index.php?rest_route=\/wp\/v2\/media\/92568"}],"wp:attachment":[{"href":"https:\/\/fivemor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=92567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fivemor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=92567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fivemor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=92567"},{"taxonomy":"dealstore","embeddable":true,"href":"https:\/\/fivemor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fdealstore&post=92567"},{"taxonomy":"offerexpiration","embeddable":true,"href":"https:\/\/fivemor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fofferexpiration&post=92567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}