Trying to pull Email into GTM DataLayer

SOLVE
Highlighted
Occasional Contributor

I need to get a user's email address into the DataLayer when they load the website, so that, depending on what they engage with, I can use the Hubl API to push them into the proper automation (and I am doing this because I could not find a way to directly add a person into automation with a command from the executed from the DataLayer).  It feels like I am missing a piece of code in this, but I am not sure where.

 

 

	<script>
  dataLayer = [];
</script>
	
<!-- Start of HubSpot Embed Code -->
<script type="text/javascript" id="hs-script-loader" async defer src="//js.hs-scripts.com/12345678.js"></script>
<!-- End of HubSpot Embed Code -->	
	
	

<script>
  var dataLayer = [{
    email: "{{ contact.email }}",
    hsgeo: "{{ contact.geographic_territory_hubspot_combined }}",
  }]
</script>
	<!-- Google Tag Manager -->

 

 

Reply
0 Upvotes
1 Accepted solution

Accepted Solutions
Highlighted
HubSpot Moderator

Hey @mikehutchinson,

 

This page: https://sopheon.staging.wpengine.com/ looks like an external page and in this case, it looks like you're pulling contact email and geogrpahic details via the personalized token: {{ contact.email }} and {{ contact.geographic_territory_hubspot_combined }}. Currently, it is not possible to use personalized toke on external page and hence this is likely why you're not seeing the value populated on the page.

Reply
0 Upvotes
7 Replies 7
Highlighted
HubSpot Moderator

Hey @mikehutchinson,

 

Digging further into this, it looks like the recommended method to put data in the dataLayer is something like this:

 

<script>
window.dataLayer.push({
 'formLocation': 'footer',
 'event': 'new_subscriber'
 });
</script>

Referencing this article: GTM Guide: dataLayer.push with examples - Analytics Mania

 

Based on your code, it looks like you're missing out the .push and it is also reocmmended to use the "window" prefix (window.dataLayer.push).

 

Could you try and see if this works?

Reply
0 Upvotes
Highlighted
Occasional Contributor

Same thing, it still just shows the values as follows:

 

{{ contact.email }}

{{ contact.geographic_territory_hubspot_combined }}

 

Reply
0 Upvotes
Highlighted
HubSpot Moderator

Hey @mikehutchinson,

 

Could you share with me the HubSpot page in question?

 

I'd like to dig into this deeper on how you're setting the contact token. 

Reply
0 Upvotes
Highlighted
Occasional Contributor

I am not pulling this from a HubSpot page, I am trying to pull it from my home page which should also be covered by the HubSpot code and cookie

Reply
0 Upvotes
Highlighted
HubSpot Moderator

Hey @mikehutchinson,

 

Thank you for the clarification. Could you share with me the page url in question? 

Reply
0 Upvotes
Highlighted
Occasional Contributor

Wendy,  I currently am doing my testing here:  https://sopheon.staging.wpengine.com/

Reply
0 Upvotes
Highlighted
HubSpot Moderator

Hey @mikehutchinson,

 

This page: https://sopheon.staging.wpengine.com/ looks like an external page and in this case, it looks like you're pulling contact email and geogrpahic details via the personalized token: {{ contact.email }} and {{ contact.geographic_territory_hubspot_combined }}. Currently, it is not possible to use personalized toke on external page and hence this is likely why you're not seeing the value populated on the page.

Reply
0 Upvotes