最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google Tag Manager - read cookie just after they are set - Stack Overflow

programmeradmin4浏览0评论

I need to read cookie that are set by Google Tag Manager.

Right now if Google Tag Manager is loaded first time for end-user I can't read cookies (because it happens later, but not immediately).

Is there a good way (callback?) that can help me to read cookies once they are set? This issue is only relevant if user visits page first time.

I need to read cookie that are set by Google Tag Manager.

Right now if Google Tag Manager is loaded first time for end-user I can't read cookies (because it happens later, but not immediately).

Is there a good way (callback?) that can help me to read cookies once they are set? This issue is only relevant if user visits page first time.

Share Improve this question asked Apr 15, 2015 at 10:32 Dmytro PastovenskyiDmytro Pastovenskyi 5,4396 gold badges40 silver badges58 bronze badges 5
  • you can use the pageload event like in this question stackoverflow./questions/16552560/… – ozkanpakdil Commented Apr 15, 2015 at 11:01
  • Are the cookies you want to read set from withing a Trigger in Google Analytics and/or from a third-party script? – Philippe Sawicki Commented Apr 17, 2015 at 23:53
  • @PhilippeSawicki they are from Google Analytics – Dmytro Pastovenskyi Commented Apr 19, 2015 at 9:09
  • If I understand correctly, you would like to read the __utm* or _ga cookies? – Philippe Sawicki Commented Apr 20, 2015 at 13:16
  • @PhilippeSawicki _ga – Dmytro Pastovenskyi Commented Apr 21, 2015 at 13:41
Add a ment  | 

1 Answer 1

Reset to default 4

From the discussion in the ments below the question, I would suggest the following solution (assuming you also send Pageviews to Google Analytics). Examples are based on Simo Ahava's (excellent) blog.

(Please note that I haven't had the opportunity to test it thoroughly, as I am currently unable to create a plete test case for this setup -- there might be some hiccups)

1. Create a Variable to read the _ga cookie

Reference: https://www.simoahava./analytics/macro-magic-google-tag-manager/#1-client-time

This will return the value for the cookie with name specified (be careful in where you use the value read, as there is always a chance that cookies are rejected by the User's browser policies).

Create a new Variable, with:

  • Variable Name of GA Cookie
  • Cookie Name of _ga


(source: simoahava.)

2. Define a callBackFunction

Reference: https://www.simoahava./analytics/macro-magic-google-tag-manager/#8-hitcallback-with-a-universal-analytics-tag & https://www.simoahava./analytics/macro-magic-google-tag-manager/#6-get-clientid-using-_ga-cookie

This callback function will be executed right after the Pageview is sent (i.e. after the _ga cookie is set).

Create a Custom JavaScript Macro with the following code:

function () {
   return function () {
      // Code to be executed in order to read the cookie:
      try {
         var gaCookie = {{GA Cookie}};

         // Do what you need to with the cookie here:
         // ...

         return gaCookie;
      } catch (e) {
         console.log('No Universal Analytics cookie found.');
         return 'N/A';
      }
   }
}

3. Define a hitCallback after sending the Pageview

Reference: https://www.simoahava./analytics/macro-magic-google-tag-manager/#8-hitcallback-with-a-universal-analytics-tag

This will execute the callback function you just defined right after the Pageview is sent.

In the Fields to set option of your Pageview Tag, set:

  • Field Name to hitCallback
  • Value to {{callBackFunction}}


Hope it can help you move forward.

发布评论

评论列表(0)

  1. 暂无评论