Create contact via JavaScript Events API with GTM


#1

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,


#3

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.


#4

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!


#5

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.


#8

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

How do you paste code?

Thanks


#9

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

Code%20block%20in%20Discourse


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

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

#11

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