{"id":6,"date":"2017-01-24T13:09:25","date_gmt":"2017-01-24T13:09:25","guid":{"rendered":"http:\/\/gaconnector.com\/support\/?p=6"},"modified":"2020-05-21T06:40:45","modified_gmt":"2020-05-21T06:40:45","slug":"how-to-save-google-analytics-client-id-cookie-to-salesforce","status":"publish","type":"post","link":"https:\/\/gaconnector.com\/docs\/how-to-save-google-analytics-client-id-cookie-to-salesforce\/","title":{"rendered":"Save Google Analytics Client ID cookie to Salesforce"},"content":{"rendered":"<p><em>For the following systems, we have special instruction for saving GA Client ID to Salesforce:<\/em><\/p>\n<ul>\n<li><em><a href=\"https:\/\/gaconnector.com\/docs\/how-to-configure-marketo-to-save-ga-client-id-to-salesforce\/\">Marketo<\/a><\/em><\/li>\n<li><em><a href=\"https:\/\/gaconnector.com\/docs\/how-to-configure-pardot-to-save-ga-client-id-to-salesforce\/\">Pardot<\/a><\/em><\/li>\n<\/ul>\n<p><em>If you don&#8217;t use either of those, this guide is for you:<\/em><\/p>\n<h2>Overview<\/h2>\n<p>In order to send offline sales stats from Salesforce to Google Analytics, we need to know which lead\/account\/opportunity\/booking belongs to which website visitor in Google Analytics.<\/p>\n<p><strong>This can be achieved by saving the ID of each website visitor to Salesforce field\u00a0zuevsoftware__gaconnector_Google_Analytics_Client_ID__c<\/strong> during the form submission.<\/p>\n<p>This ID (Google Analytics Client ID, or GA CID) should be saved to Salesforce Lead object,\u00a0alongside the normal form submission data like phone, name or email address.<\/p>\n<p>This ID normally looks like two long numbers separated by a dot, for example: <em>1233979415.1478058607<\/em><\/p>\n<p>There are ways we can retrieve CID:<\/p>\n<ol>\n<li>Either using JavaScript, by calling analytics.js methods.<\/li>\n<li>Or on the server-side, by simply retrieving the cookie that contains CID.<\/li>\n<\/ol>\n<h2><strong>Method #1 &#8211; Retrieving\u00a0CID from JavaScript<\/strong><\/h2>\n<p>Here is the code snippet that can be used to retrieve client ID:<\/p>\n<pre class=\"lang:default decode:true \">var clientId = ga.getAll()[0].get('clientId');<\/pre>\n<p>This code should be placed after Google Analytics code, as it won&#8217;t work\u00a0unless GA tracker is already initialized.<\/p>\n<h2><strong>Method #2 &#8211; Retrieving\u00a0CID on the server-side<\/strong><\/h2>\n<p>Client ID is stored in browser cookies, which is why it can also be retrieved on the server-side.<\/p>\n<p>The name of the cookie that contains CID is <em>_ga:<\/em><\/p>\n<p><img decoding=\"async\" class=\"alignnone\" src=\"https:\/\/i.gyazo.com\/e93ec5f569217f812749369552e89d9c.png\" alt=\"The name of the cookie that contains CID is _ga\" width=\"640\" height=\"162\" \/><\/p>\n<p>The only problem is that _ga cookie contains some extra information except for GA CID itself.<\/p>\n<p>Here is a sample value that is stored in _ga cookie:\u00a0<em>GA1.2.1233979415.1478058607<\/em><\/p>\n<p>1233979415.1478058607 is the actual CID, while\u00a0<em>GA1.2.<\/em> is just prefix that contains versioning number and cookie domain level.<\/p>\n<p>We need to remove the prefix and only send the second half of the CID to Salesforce.<\/p>\n<p>Here is the code example (in PHP) for retrieving CID on the sever-side:<\/p>\n<pre class=\"lang:default decode:true\">&lt;?php\r\n$cid = preg_replace('\/GA[0-9]+\\.[0-9]+\\.\/', '', $_COOKIE['_ga']);\r\n?&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p>The JavaScript method is more preferable, as:<\/p>\n<blockquote><p><em>You should not directly access the cookie analytics.js sets, as the cookie format might change in the future <\/em>(from <a href=\"https:\/\/developers.google.com\/analytics\/devguides\/collection\/analyticsjs\/cookies-user-id#getting_the_client_id_from_the_cookie\">analytics.js documentation<\/a>).<\/p><\/blockquote>\n<p>But not to worry, the server-side cookie method has been working for a long long time and will probably keep working for a long-long time. gaconnector.com website relies on this method\u00a0too.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For the following systems, we have special instruction for saving GA Client ID to Salesforce: Marketo Pardot If you don&#8217;t use either of those, this guide is for you: Overview In order to send offline sales stats from Salesforce to Google Analytics, we need to know which lead\/account\/opportunity\/booking belongs to which website visitor in Google [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"_links":{"self":[{"href":"https:\/\/gaconnector.com\/docs\/wp-json\/wp\/v2\/posts\/6"}],"collection":[{"href":"https:\/\/gaconnector.com\/docs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gaconnector.com\/docs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gaconnector.com\/docs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gaconnector.com\/docs\/wp-json\/wp\/v2\/comments?post=6"}],"version-history":[{"count":10,"href":"https:\/\/gaconnector.com\/docs\/wp-json\/wp\/v2\/posts\/6\/revisions"}],"predecessor-version":[{"id":2127,"href":"https:\/\/gaconnector.com\/docs\/wp-json\/wp\/v2\/posts\/6\/revisions\/2127"}],"wp:attachment":[{"href":"https:\/\/gaconnector.com\/docs\/wp-json\/wp\/v2\/media?parent=6"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gaconnector.com\/docs\/wp-json\/wp\/v2\/categories?post=6"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gaconnector.com\/docs\/wp-json\/wp\/v2\/tags?post=6"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}