Google Analytics : different values of totalUsers in Web and n8n

I’m facing an issue with Analytics. My workflow is supposed to be triggered everyday at 8am (UTC+01:00) and gets the total number of users that visited my website (I set the settings for “Yesterday”).

I also set a condition that returns true if this total number is 1. When true is returned, it sends an email that says there is only one visitor on my website. It is a very simple workflow, but still doesn’t work correctly.

Sometimes it sends the email whereas there is more than 1 visitor. I don’t really get why.

  • n8n version: 1.64.3
  • Database (default: SQLite): SQLite
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker

Hi @NitNeuq22

Thanks for posting here and welcome to the community! :raised_hands:

For debugging, it would help if you could have a look at the execution where an email was sent despite more than 1 visitors and inspect the data that was received from Google Analytics.

Can you share some data from that flow to look at how it get’s evaluated along the way?
(make sure to remove any sensitive content)

Tip for sharing your workflow in the forum

Pasting your n8n workflow


Ensure to copy your n8n workflow and paste it in the code block, that is in between the pairs of triple backticks, which also could be achieved by clicking </> (preformatted text) in the editor and pasting in your workflow.

```
<your workflow>
```

Make sure that you’ve removed any sensitive information from your workflow and include dummy data or pinned data as much as you can!


Hi !

Thank you for your answer.

When the email is sent, the Analytics node only gets 1 user, whereas, in this case, they were 2.

image

I already tried to change the hour of execution, to be later (at first I thought it was the time difference, because I’m in France GMT +1, but even when changing the hour of execution, it still sends the emails)

Can you please share the output of your google analytics node?
I am not sure if the error may come from how the data is processed further down the stream or if the data is incorrectly fetched from the node itself.

Thanks!

Tip for sharing your workflow in the forum

Pasting your n8n workflow


Ensure to copy your n8n workflow and paste it in the code block, that is in between the pairs of triple backticks, which also could be achieved by clicking </> (preformatted text) in the editor and pasting in your workflow.

```
<your workflow>
```

Make sure that you’ve removed any sensitive information from your workflow and include dummy data or pinned data as much as you can!


Yes ! Here it is :

[
{
“date”: “20241209”,
“totalUsers”: “1”
}
]

It is indeed the data that is incorrectly fetched by the node, because as you can see in my previous reply, Analytics shows 2 users whereas the node gets only 1, i don’t know how the data is fetched but it doesn’t work for me :smiling_face_with_tear:

Hi !

Do you have any idea of what could’ve happened? Probably a problem from the Analaytics node?

I actually found out that if totalUsers is equal to 0, then the data is not returned in the output since there is no other column with informations. I successfully added the column (“28 day active users”, these informations are useless to me but it adds the column), so now, the output (if 0) is returned correctly. This part is fixed, but I don’t know if this was the part that caused my principal issue.

I will try this way, and come back if it still doesn’t work

@NitNeuq22 Can you share your workflow?

I’ve been doing these analytics workflows and never experienced any challenges or inaccurate data… The issue might be your IF logic

Hi !

Here it is :

  "meta": {
    "instanceId": "acaf48b5e5942fb5b7f5f92a6eea286196e1eb2dacd59a5a48c1fd87412e3081"
  },
  "nodes": [
    {
      "parameters": {
        "propertyId": {
          "__rl": true,
          "value": "*my_analytics_url*",
          "mode": "url"
        },
        "dateRange": "yesterday",
        "metricsGA4": {
          "metricValues": [
            {},
            {
              "listName": "active28DayUsers"
            }
          ]
        },
        "dimensionsGA4": {
          "dimensionValues": [
            {}
          ]
        },
        "returnAll": true,
        "additionalFields": {}
      },
      "id": "373add59-3985-4519-84cb-9ef3fddb65e8",
      "name": "Récupérer le nombre de visiteurs de la veille",
      "type": "n8n-nodes-base.googleAnalytics",
      "typeVersion": 2,
      "position": [
        260,
        620
      ],
      "alwaysOutputData": true,
      "credentials": {
        "googleAnalyticsOAuth2": {
          "id": "*my_analytics_id*",
          "name": "Google Analytics account"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "66d003d7-690d-4d37-a709-77fcd442b531",
              "leftValue": "={{ $json.totalUsers }}",
              "rightValue": "=1",
              "operator": {
                "type": "string",
                "operation": "equals",
                "name": "filter.operator.equals"
              }
            }
          ],
          "combinator": "and"
        },
        "looseTypeValidation": true,
        "options": {}
      },
      "id": "8ccbcce5-0a74-41e8-98c5-eb1c6c084ab6",
      "name": "Condition date + nb visiteurs",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        500,
        620
      ]
    },
    {
      "parameters": {
        "sendTo": "[email protected]",
        "subject": "=Site Dury Artisans Bois : Analytics détecte une seule visite ({{ $json.formattedDate }})",
        "message": "=Bonjour,<br><br>\n\nCe message est généré automatiquement suite à une analyse du compte Google Analytics pour le site https://www.dury-artisan-bois.fr/.  <br><br>\n\nNous avons remarqué qu'une seule visite a été détectée sur le site le {{ $json.formattedDate }}. Veuillez vérifier que le lien avec le site s'effectue toujours.<br><br>\n\n<hr>\n<br>\nVotre assistant N8N / Google Analytics",
        "options": {
          "appendAttribution": false,
          "senderName": "RV - N8N Automatisation"
        }
      },
      "id": "301bc796-6605-441c-97f3-a79e6c314935",
      "name": "Envoie un mail",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        1000,
        620
      ],
      "webhookId": "faa8b371-3941-4072-bb53-d8ed23d45bc9",
      "credentials": {
        "gmailOAuth2": {
          "id": "*my_gmail_credentials*",
          "name": "Gmail account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Loop over input items and add a new field called 'formattedDate' to the JSON of each one\nfor (const item of $input.all()) {\n  const rawDate = item.json.date; // Remplace 'dateField' par le nom du champ contenant la date\n  \n  if (rawDate && rawDate.length === 8) {\n    const year = rawDate.substring(0, 4);\n    const month = rawDate.substring(4, 6);\n    const day = rawDate.substring(6, 8);\n    \n    // Reformater la date au format DD/MM/YYYY\n    item.json.formattedDate = `${day}/${month}/${year}`;\n  } else {\n    item.json.formattedDate = 'Invalid date'; // Gérer les cas où le format est incorrect\n  }\n}\n\nreturn $input.all();\n"
      },
      "id": "98548655-e64b-4608-bde2-4954ff774b3b",
      "name": "Code",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        500,
        480
      ]
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "triggerAtHour": 9
            }
          ]
        }
      },
      "id": "dde4f737-caaa-432d-b45d-31a786c05964",
      "name": "Lancement à 8h du matin",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        620
      ]
    },
    {
      "parameters": {
        "mode": "combine",
        "advanced": true,
        "mergeByFields": {
          "values": [
            {
              "field1": "date",
              "field2": "date"
            }
          ]
        },
        "options": {}
      },
      "id": "61962a85-16c0-4d9d-9ca2-39c506603d56",
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3,
      "position": [
        780,
        620
      ]
    }
  ],
  "connections": {
    "Récupérer le nombre de visiteurs de la veille": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          },
          {
            "node": "Condition date + nb visiteurs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Condition date + nb visiteurs": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Code": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Lancement à 8h du matin": {
      "main": [
        [
          {
            "node": "Récupérer le nombre de visiteurs de la veille",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Envoie un mail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {}
}

That cant be copied

Can you try this format

3 backticks (`) paste your code and then 3 more when done

It will give something like this

I edited my previous answer, you should be able to copy the code now

Its not working

Well, I assume this is because I didn’t post the ids to authenticate, so it’s quite normal. Maybe you can test it with ids and urls from your analytics account ?

No, the issue is you are pasting the workflow wrongly

I guess it’s okay now ?

I’ve set the flow again., With a new logic to set the date time rather than use code

1 Like

I will try this ! Thank you

Hi !

Unfortunately it still doesn’t work. When I look directly the Google Analytics property, it shows for example 1 total user, but the N8N execution shows 0 for the same date. Is “Total User” the right element to choose ? I saw “1 Day Active Users”, maybe this choice is better for the situation ?

You must be doing something wrong… I have been testing this and could get accurate results for more than 10 calls or so

If you are looking for real-time Active users, the API may be late in reporting… Opt for an RPA over the API for realtime active users

What are the differences between RPA and API ?

I think I actually successfully connected the analytics to my email. I used “1 Day Active Users” instead of “Total Users” like I said in the previous message, and it seems to work now ! :slight_smile:

Thank you for your help !

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.