How to connect GA4 and Zapier with Google Sheets + Apps Script

In today’s article, I wanted to talk about a workaround I recently made to connect the Google Analytics 4 measurement protocol (GA4 Measurement Protocol) with Zapier to send conversion events to Analytics even if they have not taken place neither on our website nor in our app that we are measuring.

1. The basics of connecting GA4 and Zapier with Google Sheets + Apps Script.

To get familiar with the GA4 measurement protocol, it is best to watch this video by Julius Federovicius, from Analytics Mania, which explains step by step how to successfully send an event through the GA4 event builder.

On the other hand, this other tutorial on how to use the Measurement Protocol of Google Analytics 4 made by Michele Pisani is the main inspiration for this solution. From there I extracted the code that I then adapted according to what I needed with a little help from ChatGPT.

Why this solution to connect GA4 and Zapier?

In some advertising platforms, some of the lead generation campaigns consist of publishing ads and collecting contact information directly on these platforms with a form with preloaded data and without the need to browse your website. This is the case of LinkedIn with Lead Gen Forms or Facebook with Lead Ads.

This has the advantage that lead generation is much more effective, since the user does not have to navigate through an unknown website or enter their data manually in a form. Unfortunately, this makes it difficult to measure results, because since they do not land on our website, we cannot track their activity with the Google Analytics 4 pixel.

That is why we use tools such as Zapier, which is an automation platform that allows users to connect applications and web services to automate repetitive tasks by creating “Zaps”, automated workflows that efficiently move data between applications. These “Zaps” consist of triggers and actions, so that when an event (trigger) occurs, an action is generated.

In our case, it would be something like this:

  • Every time a LinkedIn Lead Gen Forms or Facebook Lead Ads form (trigger) arrives,
  • Then send a conversion to Google Analytics 4 (action).

Easy, isn’t it? Apparently, but it will not be so simple, because you can not add parameters to this event, nor a user id that allows you to trace the user’s navigation in subsequent visits to your website. As of the publication of this article, the Zapier connector with GA4 does not let you do more, and using a “custom webhook” connector to send this HTTP call has not given me any guarantee that it will be executed correctly and regularly. This measurement protocol is quite delicate, and at the slightest touch it makes that call fail.

Then, the solution will be to use a Google Spreadsheet, which will serve as a pivot to send the information from the advertising platform to GA4:

Let us then go on to configure each of the parties involved:

  • Google Sheets
  • Apps Script
  • Zapier

3. Setting up the Google Sheets file

In this file, we will need 2 tabs:

  • An “input” call, where the data coming from Zapier will land.
  • Another one called “output“, in which we will move the data from the “input” sheet after completing the sending of the information to GA4. I say “move”, because after each sending of information, the “input” sheet will be empty, only with the header row.

And in the columns, the fields we need to be able to send an event:

  • client_id: this is the id of the user’s device, consisting of two strings of 8 and 10 numbers, respectively, separated by a dot. For example: “40039303.1658742998”.
  • user_id: this will be the user id that will allow us to identify you on different devices, such as Chrome on your work PC or Safari on your iPhone, when you login or fill out another form on our website. For example, we can use this one, which is an encrypted email with SHA256: “45bb5ffe4de9e31aae408ef38a38a38a82de3289ea8c4344857c93d288052b5da9c”.
  • event_name: the name of the conversion event we want to send. In our case, it will be “lead”.
  • timestamp_micro: this variable is very important. As Julius commented in the tutorial I quoted at the beginning, the timestamp must be delivered in numerical format and in microseconds. For example: 1701723824000000 is the timestamp in microseconds for 04/12/2023 at 21:44:03.
  • eventParam1 (optional): this would be an additional parameter to enrich the information you send under the “event_name”. Rename it as you like and give it the value that helps you the most.
  • eventParam2 (optional): the same as with “eventParam1”.
  • page_location: here you send a page URL where you intend to simulate the conversion. For example “https://mywebsite.com/my-conversion-url/”.
  • ev_traffic: we are going to use this parameter to pass the UTM parameters, although I have not found the way to send them with this solution, at least we can pass them as event parameter. Hopefully someone will read this and contribute their solution to this. In this case, we have concatenated medium, source and campaign, separated by a pipe character “|”. For example: “paid|linkedin|my-campaign-name”.
  • user_id_cd: would be the same value we use for the “user_id” variable. We are going to use it as an example to pass user parameters in the call to GA4.
  • session_id: will be the user’s session id, which will carry the value of the timestamp, but in seconds. For example: 1701723824 (the same we used before but without the six zeros).

We will use these headers in both the “input” and “output” sheets:

Important: format the client_id column as text and the timestamp_micros column as a number without decimals or thousands separators to avoid unpleasantness:

4. Configuring Apps Script

Once the file is prepared, we are going to prepare the Apps Script code , which is to Google Sheets what VBA macros are to Excel. For it, we accede through the menu Extensions -> Apps Script:

Once here, give the project a name and create a file to enter code:

And in that file, paste the code that I attach in this text file. Locate and add your GA4 credentials (Julius explains it in his video), modify it to your liking and needs. I have left it with comments so you can follow/correct/improve it. In the screenshot above you will see a snippet of it.

What does this script do?

  • Be authenticated on your Google Analytics property 4.
  • Scroll through the contents of the columns of your Google Sheets sheet row by row.
  • For each row, it sends an event to GA4 with the information of each of the columns we have specified.
  • Clean the “input” sheet, except for the headers, and take the content to the first empty row of the “output” sheet, adding a column at the end with the date and time when the call to GA4 was made.

Important: modify the variable “nColumns” in line 16 of the screenshot according to the number of columns in your input sheet. In the case of this example, we had 10 columns.

5. Feeding the Google Sheets file from Zapier

With all this prepared, we would go on to prepare the Zap and its different tasks. Here is an example of a real Zap with 16 tasks. The tasks we are not interested in are crossed out and those that concern us are marked in red:

As you can see, task number 12 is the one with which we sent the information from the Google Analytics Universal Measurement Protocol from Zapier. This task is no longer available for newly created Zaps. . Este conector nos permitía construir la llamada HTTP de forma muy cómoda, tan solo mapeando los valores establecidos en las tareas anteriores. El conector de GA4 es mucho más limitado en ese sentido, como comentamos anteriormente. Dicho esto, pasamos a crear nuestros parámetros necesarios para construir nuestra llamada HTTP.

Let’s go to task number 13 of the previous Zap. Here, we are going to create a random 8-digit number that we will use to create the first part of our client_id. To do this, we choose:

  • App: Formatter by Zapier
  • Event: Numbers
  • Transform: Random number
  • Lower Range: 1000000 (for 8 digits, we start from the value “10 million”)
  • Upper Range: 9999999 (the highest number we can take with 8 digits)
  • Decimal points: 0

Thus, the task would look like this:

We move on to task number 14. Here, we are looking to generate a timestamp that we are going to use for:

  • Generate the second part of the client_id
  • Generate session_id
  • Generate timestamp_micros when multiplied by 1 million

Thus, we would create the task like this:

Next task, number 15. This one is simple: just multiply the timestamp obtained in the previous step by 1 million like this:

  • App: Formatter by Zapier
  • Event: Numbers
  • Transform: Spreadsheet-Style Formula
  • Formula: timestamp*1000000

The final step would be to map all these values plus the user_id and other event parameters that we want to enter in our Google spreadsheet:

We choose the Google Drive App and look for the file containing the fields and scripts. From there, we start mapping values calculated in previous steps. If there are any values that are static, such as page_location, you can even enter them by hand:

We test the action, and check that everything has arrived correctly to the Google sheet:

We go back to Apps Script and execute the code:

And finally, we check that the “input” sheet is empty after the execution of the script, as well as the “output” sheet contains the data with the date and time of the script execution:

6. Checking event arrival in GA4 real time and BigQuery

Now, we would have to check if the event and parameters has reached GA4. We can check it with the real time:

All the parameters you have sent in the call should appear. Here is a sample:

If you send a lot of events at once, GA4’s real time may not show you all of them. Don’t worry, the next day, we will also be able to check them in BigQuery:

7. Execute the Apps Script code periodically.

We have tested that the process is done correctly in the Google spreadsheet and that the event arrives to Analytics. Zapier will send the information to the sheet every time a lead arrives, but now we have to program the Google sheet to send that information to GA4 periodically.

To do this, go back to Apps Script and look for the“Activators” option in the left menu:

Once there, look for“Add activator“:

And we set the frequency. For our case, I have configured it“according to time” and by hours. You can also set it by minutes, days, weeks or months, as you wish.

8. Limitations of this solution

As I said at the beginning, this is just a fix, and it does not solve some of the limitations that already existed and adds some more:

  1. Keep an eye on task consumption in Zapier: depending on the type of license you have and the volume of conversions you want to import into GA4, keep in mind that Zapier bills per task (or “tasks”) and not per Zap. For this import, we have added 4 tasks within Zap, so multiply it by the number of conversions you expect to get through these channels each month to estimate the impact on your Zapier license.
  2. The GA4 measurement protocol does not allow to define the UTMs without a previous session in a browser today. I have tried several ways and have not been able to get it, hence I invented that “ev_traffic” parameter to simulate them. This requires additional transformation work in both BigQuery and Looker Studio if you want to correctly impute the conversions.
  3. Number of rows in Google Sheets: this applies mainly to the “output” tab that will log all the imports we make. Google sheets has a maximum of 5 million rows, so clean the log from time to time when you see that you are approaching that number and/or make backup copies periodically.
  4. Data import frequency: in our case, we have applied an hourly frequency without problems. GA4 allows you to import data via measurement protocol up to 72 hours after each event occurs, although I personally recommend you not to leave more than 48. Usually you say “ah, 72 hours is 3 days”, but then 3 days and a few hours pass and you have already lost data. So, be careful :)

And this is the end of today’s post, which has been a long one. I found this shape after months of hitting a wall. Can you think of any way to improve this solution? I would really like to hear about it, especially with the UTMs issue.

Leave a Comment