【问题标题】:Tealium Tags Integration with React JsTealium 标签与 React Js 的集成
【发布时间】:2019-06-20 17:34:25
【问题描述】:

我正在尝试将 Tealium 标签与我们基于 react.js 的项目集成;但是,我碰巧没有找到有关此问题的任何文件?如果有人可以向我提供一些关于如何做到这一点的文档或示例,我将不胜感激?

【问题讨论】:

    标签: reactjs tealium


    【解决方案1】:

    Tealium 文档有 a section on Single Page Applications 适用于 React。这个答案引用了该文档并根据我的经验添加了一些内容。

    修改 Tealium 脚本

    首先,你需要用以下两行稍微修改它们的全局脚本:

    window.utag_cfg_ovrd = window.utag_cfg_ovrd || {};
    window.utag_cfg_ovrd.noview = true;
    

    这会覆盖自动页面跟踪(因为在 SPA 中没有发生页面导航)。完整的脚本应该是这样的(修改installation guide中的代码):

    <!-- Tealium Universal Data Object -->
    <script type="text/javascript">
      var utag_data={
          "page_type"     : "section",
          "site_section"  : "men",
          "page_name"     : "Men's Fashion | Acme Inc.",
          "country_code"  : "US",
          "currency_code" : "USD"};
      window.utag_cfg_ovrd = window.utag_cfg_ovrd || {};
      window.utag_cfg_ovrd.noview = true
    </script>
    
    <!-- Tealium Universal Tag -->
    <script type="text/javascript">
      (function(a,b,c,d) {
          a='//tags.tiqcdn.com/utag/ACCOUNT/PROFILE/ENVIRONMENT/utag.js';
          b=document;c='script';d=b.createElement(c);d.src=a;
          d.type='text/java'+c;d.async=true;
          a=b.getElementsByTagName(c)[0];a.parentNode.insertBefore(d,a)})();
    </script>
    

    显式处理视图

    由于没有页面导航,您需要在进行路由的地方以编程方式调用utag.view()

    window.utag.view({ variable1:"VARIABLE1 VALUE", variable2:"VARIABLE2 VALUE", variable3:"VARIABLE3 VALUE" });
    

    处理Utag的异步加载

    如果您使用默认模式,则 utag 对象是异步加载的,您的 React 代码可能会在它完成加载之前尝试调用它。在 utag 正确加载之前,您应该有一些逻辑保护访问。下面是一些示例代码:

    提供默认 Utag

    // If you aren't worried about calling utag before it has loaded (e.g. user interactions)
    // This will try to call Tealium but will just drop the event if utag is not loaded
    
    export const utag = window.utag || { link: () => {}, view: () => {} };
    
    // Usage: just use the exported utag, don't use the utag on window
    utag.link({ ... })
    

    在 Promise 中封装 Utag 调用

    // If the utag call could possibly happen before utag has loaded (e.g. page load)
    // This will make the call as soon as utag has loaded (it still drops it if utag doesn't load in a certain amount of time)
    
    export const withUtag = async () => {
      if (window.utag) return window.utag;
      let i = 0;
      while (i < 50) {
        await new Promise(resolve => setTimeout(resolve, 200));
        if (window.utag) return window.utag;
        i = i + 1;
      }
      return { link: () => {}, view: () => {} };
    }
    
    // Usage: Use the resolved utag object from the Promise
    withUtag().then(utag => utag.view({ ... }))
    

    【讨论】:

    • 修改全局脚本的2行应该在里面吧?因为我们正在将它添加到页面的正文中。
    • @TonyRoczz 不错!
    • 您能否更新答案,您能否与上述代码共享代码仓库的链接,以便更好地了解整个实现?谢谢
    • 生活对我来说非常忙碌,但我没有忘记这个请求。当我有时间时,我会加倍努力并尝试创建一个示例 repo。
    猜你喜欢
    • 2020-11-27
    • 2019-12-25
    • 1970-01-01
    • 2020-12-23
    • 1970-01-01
    • 2019-06-29
    • 2019-11-06
    • 1970-01-01
    • 2017-12-28
    相关资源
    最近更新 更多