sGTM Facebook CAPI Template Not Accepting Custom Variables

We’re trying to use a sGTM custom template variable that parses out specific parameters from the standard “items” array in event data, to send to Meta via CAPI (this has to be done in this setup).

We apply that variable to the “contents” property in the custom data list in the tag.

In preview mode - the variable produces a correct value. But the tag preview shows that same variable as “undefined”, and the tag hangs/fails.

Is the template tag not able to accept custom template variables? We tested with “constant” variables and it worked fine, so that’s our assumption.

Hi @brett, the tag accepts the contents just fine. Check the screenshots below.

For this example, I used the Universal Conversions Variable template that picks a GA4 items array and transforms into a FB CAPI contents array.

contents in the outgoing request to Facebook.

Please, share more details and screenshots from your setup.
Are you performing some sort of external request to enrich the items before transforming into contents? If so, this can explain why the tag preview shows undefined while Variable tab shows the expected value.

Hi Giovani - thanks for the reply. We’re using a custom template variable to read the “items” array and filter it so it only sends item_id, price, and quantity (it’s necessary for this client).

So the output of the variable is something like this: “[{\“id\”:\“WZSN1SG2\”,\“quantity\”:1,\“item_price\”:169}]”

In sGTM preview - the variable shows that value correctly in the “variables” tab. But in the tag, it shows as “undefined”. We tried the same for another variable that just outputs a numerical value, same thing.

So it looks like the Custom Data list in the tag possibly does not accept variables built from Custom Templates. When we tested with a “Constant” variable" it worked fine.

Hi @brett.

So it looks like the Custom Data list in the tag possibly does not accept variables built from Custom Templates. When we tested with a “Constant” variable" it worked fine.

In the screenshot from this message sGTM Facebook CAPI Template Not Accepting Custom Variables - #2 by giovaniortolani I used a custom template, so that’s not the problem.

If possible, please give me access to the container (get@stape.io). I’ll take a look.
Alternatively, you can send me the .json file with the bare minimum (only the FB CAPI tag and the said custom variable).

I can’t grant container access. What’s the best way to send the JSON?

@brett, you can upload it somewhere (Google Drive etc.) and DM me here the URL.

Sounds good - here you go

Summary

https://drive.google.com/file/d/1zACjlU5KhZa6oUrxJiW5tR-kub4POQhM/view?usp=sharing

Glad to share anything else needed to get this to work!

Hi @brett, I don’t see anything wrong with the container you sent me. You can now delete it.

The `contents` variable is passed just fine to the tag and to the outgoing request. Check the screenshots below.

  • GA4 `items` array from the incoming request to the server:

  • Variable evaluation after transforming the GA4 `items` into `contents`:

  • Tag receives the `contents` value from the variable:

  • Outgoing request sends the expected `contents` array from the variable:

what has me concerned is that in GTM preview for the tag, it’s showing “contents” as “undefined”>

Why would that be with what you’re seeing?

Do you have any Transformation that removes the `items` from the Event Data?

And are you sure that the incoming GA4 event that triggers the Facebook tag from the screenshot contains the `items`?

That would be 2 cases that would produce what you see.