You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Google Tag Manager中拆分自定义JS返回对象至变量的问题

Got it, let's walk through exactly how to split each property from your adServer function's return object into separate Google Tag Manager variables. This is a common workflow, and it's pretty straightforward once you know the steps:

Step 1: Confirm your adServer function is set up as a GTM variable

First, I assume you've already created a Custom JavaScript variable in GTM for your adServer function. If you haven't yet, here's a quick setup:

  • Navigate to Variables > User-Defined Variables > New
  • Select "Custom JavaScript" as the variable type
  • Paste your full adServer function code (the one that returns the object with lng, version, etc.)
  • Name it something clear like adServer Full Data (this is the variable we'll reference for all property extractions)
Step 2: Create individual variables for each property

For every attribute in the returned object (lng, version, os, tab, utm_campaign), you'll make a separate Custom JavaScript variable that pulls that specific value. Here's a template you can reuse for all properties:

Example: Create a variable for lng

  1. Go to Variables > User-Defined Variables > New
  2. Choose "Custom JavaScript" as the type
  3. Paste this code (replace {{adServer Full Data}} with the exact name of your main adServer variable):
    function() {
      // Grab the full object from your adServer variable
      var adServerData = {{adServer Full Data}};
      // Return the target property, or undefined if the object doesn't exist (avoids errors)
      return adServerData ? adServerData.lng : undefined;
    }
    
  4. Name the variable adServer - lng (use a consistent naming convention for easy tracking)

Repeat this process for each remaining property:

  • For version: Swap adServerData.lng with adServerData.version
  • For os: Swap with adServerData.os
  • For tab: Swap with adServerData.tab
  • For utm_campaign: Swap with adServerData.utm_campaign

The adServerData ? ... : undefined check is key—it prevents GTM from throwing errors if your adServer function fails to return a valid object (e.g., if it loads late or encounters a runtime issue).

Step 3: Test your variables in GTM Preview Mode

Once all variables are created, enable GTM's Preview mode and trigger the script that runs your adServer function. In the Preview pane, navigate to the "Variables" tab—you should see each of your new variables populated with the corresponding value from the returned object.

If any variable shows up as undefined, double-check:

  • That your main adServer variable is correctly returning the full object
  • That you spelled the property name exactly as it appears in the object (it's case-sensitive!)
  • That the script triggering adServer runs before these variables are used in tags

内容的提问来源于stack exchange,提问作者Pak Hang Leung

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:19:17