APIs & Integrations

JaviRevillaCoya
Participant

Create contact via JavaScript Events API with GTM

SOLVE

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,

0 Upvotes
1 Accepted solution
JaviRevillaCoya
Solution
Participant

Create contact via JavaScript Events API with GTM

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

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

View solution in original post

0 Upvotes
7 Replies 7
IsaacTakushi
HubSpot Employee
HubSpot Employee

Create contact via JavaScript Events API with GTM

SOLVE

Excellent, thank you. I forgot to close the _hsq.push method :roll_eyes:

Isaac Takushi

Associate Certification Manager
0 Upvotes
JaviRevillaCoya
Solution
Participant

Create contact via JavaScript Events API with GTM

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

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

Create contact via JavaScript Events API with GTM

SOLVE

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

Code%20block%20in%20Discourse

Isaac Takushi

Associate Certification Manager
0 Upvotes
IsaacTakushi
HubSpot Employee
HubSpot Employee

Create contact via JavaScript Events API with GTM

SOLVE

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 and trackEvent 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:

<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 covers most of the concepts.

Isaac Takushi

Associate Certification Manager
0 Upvotes
JaviRevillaCoya
Participant

Create contact via JavaScript Events API with GTM

SOLVE

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!

0 Upvotes
IsaacTakushi
HubSpot Employee
HubSpot Employee

Create contact via JavaScript Events API with GTM

SOLVE

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.

Isaac Takushi

Associate Certification Manager
0 Upvotes
JaviRevillaCoya
Participant

Create contact via JavaScript Events API with GTM

SOLVE

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

How do you paste code?

Thanks

0 Upvotes