# postStreamの初期JSONオブジェクトをGTMのデータレイヤーに渡すことは可能ですか？

**URL:** https://meta.discourse.org/t/is-it-somehow-possible-to-pass-the-initial-poststream-json-object-into-the-gtm-datalayer/161035
**Category:** Development
**Created:** [2020 年 8 月 16 日午後 1:50 UTC](https://meta.discourse.org/t/is-it-somehow-possible-to-pass-the-initial-poststream-json-object-into-the-gtm-datalayer/161035 "2020-08-16T13:50:00Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Terrapop](https://avatars.discourse-cdn.com/v4/letter/t/ecd19e/32.png) [@Terrapop](https://meta.discourse.org/u/Terrapop)
#### Post date: [2020 年 8 月 16 日午後 1:50 UTC](https://meta.discourse.org/t/is-it-somehow-possible-to-pass-the-initial-poststream-json-object-into-the-gtm-datalayer/161035/1 "2020-08-16T13:50:00Z")

</div>

現在の悪い回避策のように、別の AJAX リクエストを行わずに、トピックページの初期投稿ストリームオブジェクトを GTM データレイヤーに渡す方法はありますか？

```
<script type="text/discourse-plugin" version="0.8">
const currentUser = api.getCurrentUser();
let userId;

if (currentUser) {
    userId = currentUser.id;
}
else
{
    userId = 0;
}

var getJSON = function(url, callback) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.responseType = 'json';
    xhr.onload = function() {
      var status = xhr.status;
      if (status === 200) {
        callback(null, xhr.response);
      } else {
        callback(status, xhr.response);
      }
    };
    xhr.send();
};

api.onPageChange((url, title) => {
    if (url.match(/^\/t\//)) {

	    getJSON(url + '.json',
        function(err, data) {
          if (err !== null) {
              
            window.dataLayer.push({
                'event': 'pageChangeData',
                'userId': userId,
                'PageType': 'topic',
                'PageJson': false
            });
            
          } else {
        
            window.dataLayer.push({
                'event': 'pageChangeData',
                'userId': userId,
                'PageType': 'topic',
                'PageJson': data
            });
          }
          
        });
	    
	} else {
	    
	        window.dataLayer.push({
                'event': 'pageChangeData',
                'userId': userId,
                'PageType': 'notopic',
                'PageJson': false
            });
	}

});
</script>

```

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [2020 年 8 月 18 日午前 12:49 UTC](https://meta.discourse.org/t/is-it-somehow-possible-to-pass-the-initial-poststream-json-object-into-the-gtm-datalayer/161035/2 "2020-08-18T00:49:57Z")

</div>

> [@Terrapop](#):
>
> トピックページの初期投稿ストリームオブジェクトを、現在私が悪い回避策として行っているような別の AJAX リクエストを行わずに GTM データレイヤーに渡すことは可能でしょうか？

Discourse イベントに関連するデータを送信しようとしている場合は、この投稿で説明されているアプローチを使用できるかもしれません: [Integrating Google Tag Manager with Google Analytics - #44](https://meta.discourse.org/t/setup-google-tag-manager-for-analytics/47335/44?u=simon_cossar).

以下のイベントのいずれかを捕捉しようとしている場合、機能するはずです:

- `post:created`
- `topic-notifications-button:changed`
- `topic:created`
- `page:bookmark-post-toggled`
- `page:like-toggled`
