如何从GTM自定义标记模板发送POST请求? [英] How to send a POST request from GTM custom tag template?

查看:122
本文介绍了如何从GTM自定义标记模板发送POST请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Google跟踪代码管理器开发一个简单的自定义代码模板.它应该绑定到某些事件,然后在POST请求的主体中以JSON的形式将事件数据发送到我们的服务器.

I'm developing a simple custom tag template for Google Tag Manager. It's supposed to bind to some events and send event data to our servers as JSON in the body of a POST request.

装有沙盒的 GTM Javascript运行时提供了 sendPixel() API.但是,这仅提供GET请求.

The sandboxed GTM Javascript runtime provides the sendPixel() API. However, that only provides GET requests.

如何在此沙盒运行时中发送POST请求?

How one sends a POST request from within this sandboxed runtime?

推荐答案

您可以结合使用injectScript和copyFromWindow API,在这里

You can use a combination of the injectScript and copyFromWindow APIs found here Custom Template APIs.

基本上,工作流程是这样的.

Basically, the workflow goes like this.

  1. 构建一个简单的脚本,其中包含附加到发送正常XHR发布请求的window对象的函数.我制作和使用的脚本可以在以下位置找到: https://storage.googleapis.com /common-scripts/basicMethods.js
  2. 将该脚本上传到可公共访问的位置,以便您可以将其导入模板.
  3. 使用injectScript API将脚本添加到您的自定义模板.
  4. injectScript API希望您提供一个onSuccess回调函数.在该函数中,使用copyWindow api获取在脚本中创建的发布请求函数并将其另存为变量.
  5. 您现在可以使用该变量来发送发布请求,就像使用普通的JS函数一样.
  1. Build a simple script that contains a function attached to the window object that sends a normal XHR post request. The script I made and use can be found here: https://storage.googleapis.com/common-scripts/basicMethods.js
  2. Upload that script somewhere publically accessible so you can import it into your template.
  3. Use the injectScript API to add the script to your custom template.
  4. The injectScript API wants you to provide an onSuccess callback function. Within that function, use the copyWindow api to grab the post request function you created in your script and save it as a variable.
  5. You can now use that variable to send a post request the same way you would use a normal JS function.

我上面包含的脚本还包括JSON编码和Base64编码函数,您可以通过copyWindow api使用相同的方法.

The script I included above also includes JSON encode and Base64 encode functions which you can use the same way via the copyWindow api.

我希望能有所帮助.如果您需要某些特定的代码示例,我可以为您提供帮助.

I hope that helps. If you need some specific code examples for parts I can help.

这篇关于如何从GTM自定义标记模板发送POST请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆