# Create contact via JavaScript Events API with GTM

**URL:** <https://community.hubspot.com/t/create-contact-via-javascript-events-api-with-gtm/11253>\
**Category:** APIs & Integrations\
**Tags:** events\
**Created:** [October 17, 2018, 7:21am UTC](https://community.hubspot.com/t/create-contact-via-javascript-events-api-with-gtm/11253 "2018-10-17T07:21:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![JaviRevillaCoya](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/javirevillacoya/32/88643_2.png) [@JaviRevillaCoya](https://community.hubspot.com/u/JaviRevillaCoya)\
**Post date:** [October 17, 2018, 7:21am UTC](https://community.hubspot.com/t/create-contact-via-javascript-events-api-with-gtm/11253/1 "2018-10-17T07:21:31Z")

</div>

Hi!

We would like to trigger the JavaScript events API from the GTM with the data that comes from the DataLayer to create a contact when a sale is made. Let’s say we can trigger this from the GTM with the visibility of one element in a page.

Can somebody help me with the code I would need to use?  
Event ID: XXXXXXXXX

Regards,

---

<div class="post-metadata">

**Author:** ![IsaacTakushi](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/isaactakushi/32/70387_2.png) [@IsaacTakushi](https://community.hubspot.com/u/IsaacTakushi)\
**Post date:** [October 18, 2018, 9:00pm UTC](https://community.hubspot.com/t/create-contact-via-javascript-events-api-with-gtm/11253/2 "2018-10-18T21:00:23Z")

</div>

Hi @javirevi,

Before we get too in the weeds, are there reasons why you wish to go through Google Tag Manager instead of implementing the [identify](https://developers.hubspot.com/docs/methods/tracking_code_api/identify_visitor) and [trackEvent](https://developers.hubspot.com/docs/methods/tracking_code_api/javascript_events_api) methods on their own?

That said, if you’re familiar with implementing custom scripts in GTM, I _think_ you would just place something like the following into the tag’s HTML field:

```auto
<script type="text/javascript">
 var _hsq = window._hsq = window._hsq || [];
 _hsq.push(["identify",{
 email: however_you_identify_email_from_the_dataLayer
 }
 _hsq.push(["trackEvent", {
 id: 000006477295
 }
 ]);
</script>

```

If you’re unfamiliar with implementing custom scripts in GTM, [this article](https://github.com/snowplow/snowplow/wiki/Integrating-javascript-tags-with-Google-Tag-Manager) covers most of the concepts.

---

<div class="post-metadata">

**Author:** ![JaviRevillaCoya](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/javirevillacoya/32/88643_2.png) [@JaviRevillaCoya](https://community.hubspot.com/u/JaviRevillaCoya)\
**Post date:** [October 19, 2018, 8:03am UTC](https://community.hubspot.com/t/create-contact-via-javascript-events-api-with-gtm/11253/3 "2018-10-19T08:03:48Z")

</div>

Hi Isaac,

This almost worked, there were a couple of errors but one our devs was around and fixed it. So it looks like this now:

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![IsaacTakushi](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/isaactakushi/32/70387_2.png) [@IsaacTakushi](https://community.hubspot.com/u/IsaacTakushi)\
**Post date:** [October 19, 2018, 12:03pm UTC](https://community.hubspot.com/t/create-contact-via-javascript-events-api-with-gtm/11253/4 "2018-10-19T12:03:50Z")

</div>

Hi @javirevi,

Good to hear! Did you mean to copy your code in your response? I’d love to see what you did for future reference.

---

<div class="post-metadata">

**Author:** ![JaviRevillaCoya](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/javirevillacoya/32/88643_2.png) [@JaviRevillaCoya](https://community.hubspot.com/u/JaviRevillaCoya)\
**Post date:** [October 19, 2018, 12:26pm UTC](https://community.hubspot.com/t/create-contact-via-javascript-events-api-with-gtm/11253/5 "2018-10-19T12:26:12Z")

</div>

Yes, i wanted to, I guess I didnt know how to do it:

How do you paste code?

Thanks

---

<div class="post-metadata">

**Author:** ![IsaacTakushi](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/isaactakushi/32/70387_2.png) [@IsaacTakushi](https://community.hubspot.com/u/IsaacTakushi)\
**Post date:** [October 19, 2018, 12:46pm UTC](https://community.hubspot.com/t/create-contact-via-javascript-events-api-with-gtm/11253/6 "2018-10-19T12:46:38Z")

</div>

@javirevi, you can paste a code block into the editing pane with **```**. See the example below:

 ![](https://d2qpr2jzcazej7.cloudfront.net/original/2X/2/26eb0542b8ad5541cf39f10152df6cda15dd85f4.jpeg)

---

<div class="post-metadata">

**Author:** ![JaviRevillaCoya](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/javirevillacoya/32/88643_2.png) [@JaviRevillaCoya](https://community.hubspot.com/u/JaviRevillaCoya)\
**Post date:** [October 19, 2018, 12:49pm UTC](https://community.hubspot.com/t/create-contact-via-javascript-events-api-with-gtm/11253/7 "2018-10-19T12:49:54Z")

</div>

```auto
<script type="text/javascript">
 var _hsq = window._hsq = window._hsq || [];
 _hsq.push(["identify",{
 email: "{{email}}"

 }]);
 _hsq.push(["trackEvent", {
 id: "000006477295"
 }
 ]);
</script>

```

---

<div class="post-metadata">

**Author:** ![IsaacTakushi](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/isaactakushi/32/70387_2.png) [@IsaacTakushi](https://community.hubspot.com/u/IsaacTakushi)\
**Post date:** [October 19, 2018, 12:53pm UTC](https://community.hubspot.com/t/create-contact-via-javascript-events-api-with-gtm/11253/8 "2018-10-19T12:53:51Z")

</div>

Excellent, thank you. I forgot to close the `_hsq.push` method  
 ![roll_eyes.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/6/8/682168e297589febe0e9a4c1ac1703814170501a.png)
