Contact Endpoint API integration and Google Analytics

Hello All,

We used to use the method of Submit data to a form Endpoint to send the registartion data to Hubspot. However, we started to use Contact data endpoint since we need to send registartion data 2 times due to nature of our form.

Our developers says that there will be no form submission with this approach, there will be only contact creation. So I am wondering how can I see form submission in Google tag manager? Do you have suggestions?

Normally, we used to track them like below:

gtmEventCategory: “sign-up.form”,

gtmEventAction: “form.submit”,

gtmEventLabel: “verifyMailForm”

Thanks,

Sebnem

Hey, @06401 :waving_hand: Thanks for reaching out. Let’s see if we can get the conversation going — @LMeert @Ben-M @Leonard0 do you have any insight you can share with @06401?

Thank you very much! — Jaycee

Hi Sebnem,

It is not possible to directly push data to a Google Data Studio data layer from an external script that is not executed on the webpage. The Google Data Studio data layer is a JavaScript object that is only accessible within the context of the webpage where it is loaded. However, there is a workaround that you can use to send data to Data Studio from an external script.

You can use a server-side script to collect data from your external script and then send it to Data Studio using the Data Studio API. Here’s an example of a server-side script in Node.js that collects data from an external script and sends it to a Data Studio report using the Data Studio API:

const {google} = require('googleapis');
const dataStudio = google.datastudio('v1');

const reportId = 'REPORT_ID';
const datasetId = 'DATASET_ID';

// Collect data from form submission
app.post('/form-submission', (req, res) => {
 const data = {
 category: req.body.category,
 action: req.body.action,
 label: req.body.label,
 date: new Date().toISOString()
 };

 // Authenticate with the Google API
 const auth = new google.auth.JWT(
 'your-service-account-email',
 'path/to/privatekey.pem',
 'your-private-key',
 ['https://www.googleapis.com/auth/datastudio']
 );

 // Append data to the Data Studio dataset
 dataStudio.data.append({
 auth,
 requestBody: {
 data: [data],
 datasetId,
 reportId,
 appendMode: 'APPEND'
 }
 });
});

In this example, the script is using the req.body object from an express.js application to collect data from the form submission. The script is collecting the category, action, label and date fields and the values are assigned to the corresponding keys in the data object. The date field is being set to the current date and time.

You will need to replace ‘your-service-account-email’, ‘path/to/privatekey.pem’, and ‘your-private-key’ with your own values. You also need to replace ‘REPORT_ID’ and ‘DATASET_ID’ with the IDs of the report and dataset you want to send data to.

It is important to note that in order to use this script, you will need to create a Service Account with appropriate permissions and also enable Data Studio API on your project. You may need to follow the instructions on the following link to set it up: https://developers.google.com/datastudio/api/guides/authentication

Also, the example script is based on Node.js and Google client library, but you can use any other language or library as long as it allows you to make authenticated requests to the Data Studio API.

Hi Leonard,

Thank you. However, we do not use Data Studio and i know that Data Studio gets the data from google analytics so i can assume that we can also see data in Google Analytics 4 with this method,right?

Kind regards,

Sebnem

Hi Sebnem,

I’m sorry, for some reason I mix-up GTM and Data Studio sometimes :person_facepalming:t2:‍

With an event you can send data to GTM data layer like below (client side). However if you are posting server side, you need a server-side GTM container.

I don’t have any experience with a server-side setup unfortunately. You need to check out documentation about that, here for example: Google Tag Manager - Server-side  |  Google for Developers

 window.dataLayer = window.dataLayer || [];
 window.dataLayer.push({
 'containerId': 'GTM-XXXXXXX',
 event: 'contactCreated',
 eventCategory: data.category,
 eventAction: data.action,
 eventLabel: data.label
 });