Git Product home page Git Product logo

electron-google-analytics4's Introduction

Google Analytics 4 - Measurement Protocol API

  • Supports event tracking for GA4 in Electron built apps.
  • Currently, it supports the event sending function.

NPM

npm version

Github Page - Docs

https://github.com/hajeonghun/electron-google-analytics4

Features

  • Event

Getting started

Installation

npm i electron-google-analytics4
or
yarn add electron-google-analytics4
  • Init

    Analytics(trackingID, secretKey, clientID?, sessionID?)

    import Analytics from 'electron-google-analytics4'; // esm
    // or
    const Analytics = require('electron-google-analytics4').default; // cjs
    
    const analytics = new Analytics('G-XXXXXX', 'xxxxx-xxxxx');

    Usage
    To use the Analytics module, create a new instance of the Analytics class by providing the required parameters: trackingID and secretKey. (secretKey Issuance Guide)
    The clientID and sessionID parameters are optional and can be omitted if not needed.
    clientID default: machineId (node-machine-id)
    sessionID default: uuidv4 (uuid v4)


  • Set Single parameter (custom params)

    Analytics#set(key, value)

    analytics.set('user', 'jeonghun');
  • Remove Single parameter (custom params)

    Analytics#set(key, null)

    analytics.set('user', null);
  • Set Multiple parameter (custom params)

    Analytics#setParams(obj)

    const params = {name: 'hun', age: 28};
    analytics.setParams(params);
  • Remove Multiple parameter (custom params)

    Analytics#setParams()

    analytics.setParams(); // customParams: {}
  • Set User Properties

    Analytics#setUserProperties(obj)

    analytics.setUserProperties({userId: 'ha', page_path: location.href});
  • Remove User Properties

    Analytics#setUserProperties()

    analytics.setUserProperties();
  • Send Event

    Analytics#event(eventName)

    analytics.event('page_view');
    // Return Promise

Notes

Method chaining is available.

analytics.set('name', 'jeonghun').set('age', 28);
// or
analytics.set('name', 'jeonghun').event('page_view');

Verification Steps

1. Log in to your GA4 account.
2. Navigate to the "Real-time" section from the left-hand menu.
3. Go to the "Overview" tab.
4. Verify if the real-time data is being displayed:
  - Check if the number of active users, page views, and events are updating in real-time.
  - If the data is not being displayed correctly, double-check your code and troubleshoot any issues.

MIT

electron-google-analytics4's People

Contributors

hajeonghun avatar

Stargazers

Guanghui Zhang avatar 陈佳 avatar hocgin avatar Elina Keränen avatar 김원태 avatar Zhu Liang avatar Tufik Chediak Sanchez avatar Antoine avatar Paul Huang avatar Vittorio avatar zhili avatar Jerry Lee Melton avatar Kyle avatar Zhenwei Wang avatar Alexey Prokhorov avatar  avatar Korbin avatar

Watchers

Dilshan Manathunge avatar  avatar

Forkers

kyle-go paradite

electron-google-analytics4's Issues

Error: require() of ES modules is not supported.

I m getting below error when trying to use const analytics = new Analytics('xxxxxx', 'xxxxxxxx'); ,i m using in main process by
import Analytics from 'electron-google-analytics4';

error log for reference
App threw an error during load
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\My Downloads\Project\node_modules\electron-google-analytics4\node_modules\axios\index.js
require() of ES modules is not supported.
require() of C:\My Downloads\Project\node_modules\electron-google-analytics4\node_modules\axios\index.js from C:\My Downloads\Project\node_modules\electron-google-analytics4\lib\index.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename C:\My Downloads\Project\node_modules\electron-google-analytics4\node_modules\axios\index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\My Downloads\Project\node_modules\electron-google-analytics4\node_modules\axios\package.json.

Question: Count users through measurement protocol

Hi @hajeonghun.

Event sending works fine (it's visible in the Realtime section of Analytics). But in the section "Users in last 30 minutes" there is always "0" value. Maybe an app should send a specific event to make GA4 calculate users? There was no such an issue previously with UA measurement protocol. Sure, that's not related to this library directly, but maybe somebody already solved such an issue.

Here's an example of request my app sends to GA4:

{
  "client_id": "<uuid>",
  "user_id": "<uuid>",
  "user_properties": {
    "app_name": {
      "value": "MyApp"
    },
    "app_version": {
      "value": "1.0.0"
    },
    "arch": {
      "value": "x64"
    },
    "os": {
      "value": "Linux"
    },
    "os_version": {
      "value": "6.2.13"
    }
  },
  "events": [
    {
      "name": "login",
      "params": {
        "session_id": "<uuid>",
        "method": "login_with_token"
      }
    }
  ]
}

I don't think this tool is sending user_id

Per Google Analytics docs, user_id is required in order to identify a user (to differentiate new user with an existing returning user).

It is also required when sending user_data.

fetch(`https://www.google-analytics.com/mp/collect?measurement_id=${measurement_id}&api_secret=${api_secret}`, {
  method: "POST",
  body: JSON.stringify({
    client_id: 'XXXXXXXXXX.YYYYYYYYYY',
    user_id: "XXX",
    events: [{
      name: 'purchase'
    }],
    user_data: {
      sha256_email_address: yourEmailSha256Variable,
      sha256_phone_number: yourPhoneSha256Variable,
      address: {
        sha256_first_name: yourFirstNameSha256Variable,
        sha256_last_name: yourLastNameSha256Variable,
        sha256_street: yourStreetAddressSha256Variable,
        city: yourCityVariable,
        region: yourRegionVariable,
        postal_code: yourPostalCodeVariable,
        country: yourCountryVariable
      }
    }
  })
});

However, looking at the code, it is only sending client_id, not user_id.

const payload = {
    client_id: this.clientID,
    events: [
        {
            name: eventName,
            params: {
                session_id: this.sessionID,
                ...this.customParams,
            },
        },
    ],
};

Happy to open a PR if you think this issue should be addressed.

Custom Event params

How to send custom Event Params such as event_category and event_label ? can we use setParams for this ?

Where do I add the script on my code

Hello 👋
Sorry for the noob question, but I'm new to electron and I don't understand how the "backend" and "frontend" works compared to a regular web application.

I'm supposed to just drop the import and initialisation at the root of main.js?
how does it get the pages I'm navigating in the front end? do I have to do something about it?

Thanks for your time!

how to retain Event and user data

Hi, I have set up my GA4 and currently the data appears on the realtime view however if i go to Events section or even the User Section within GA4, I do not see any data. is there a way to fix this ?
image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.