Needle - Unity Analytics

Hi everyone!

I’m developing a WebAR application using the Needle Engine and want to implement analytics to track user interactions, such as button clicks or object interactions, and log this data for analysis.

Does anyone know how to integrate custom analytics into a Needle Engine project? If custom analytics aren’t possible, does the Needle Engine provide any built-in analytics tools or features for tracking user behavior?

Thanks in advance!

Hello @Nemanja_Prvulov

Right now Needle Engine doesn’t have a builtin analytics system. We have it on the roadmap tho (and will soon publish a custom roadmap where you can vote on features).

It’s easy to integrate google analytics or e.g. Plausible Analytics tho (for example you can create a minimal Needle component that has a onPointerClick or onPointerDown event method which you add to all the buttons and objects you want to track interactions with).

export class SimpleAnalytics extends Behaviour
{
  onPointerDown(args) {
     console.log("Pointer event!", args); 
     // call analytics method here to track the event
   }
}
1 Like

Let me know how it goes and if it’s a workable solution for you now.

Hello, can you provide me with an example of how I can implement that? I’ve been trying all day, but I can’t figure out how to make the function work. For instance, we could use Google Analytics as an example.

Hi, this should be it:

export class SimpleAnalytics extends Behaviour
{
  // Make sure you have an EventSystem in your scene for these events to be called
  onPointerDown(args) { 
    console.log("PointerDown");

    // Check if google analytics is installed globally. If not make sure it's included (see https://developers.google.com/analytics/devguides/collection/ga4/events?client_type=gtag#add_events_to_your_javascript) 
    if ("gtag" in globalThis) {
        // Call the google analytics function to register your custom event
        globalThis.gtag('event', 'screen_view', {
        'app_name': 'myAppName',
        'screen_name': 'Home'
      });
    }
    else console.warn("Google analytics is not installed");

   }
}

Docs: Set up events  |  Google Analytics  |  Google for Developers

Let me know if it helps :slight_smile:

Yes, that is exactly what I have, but I don’t know how to implement it into the Unity engine without errors. Can you provide the full script that I can attach to an object in Unity to record that action?

What errors do you see?

Could you share the typescript and c# script that you currently have? I’m currently writing it here too and didn’t get any of these errors so something in your script might trip the codegen.

I don’t use typescript in my script, that is a reason why this doesnt work. That’s why i need example for whole script, I’m untiy developer, i do not know typescript.

The script i shared above is a Typescript file - it seems like you pasted the code in a C# script but that’s not working (as you can see).

  1. Delete the c# script that you created

  2. Open the web workspace and create a new typescript file in src/scripts/ and then paste your typescript code in that file (see code below)

  3. Go back to unity, a c# component will be generated. Add that component to the same gameobject as the button for example.

import { Behaviour, EventSystem, ObjectRaycaster, PointerEventData, serializable } from "@needle-tools/engine";

export class SimpleAnalytics extends Behaviour {

    @serializable()
    eventName?: string = "google-event-name";

    awake(): void {
        EventSystem.createIfNoneExists(this.context);
    }

    // Make sure you have an EventSystem in your scene for these events to be called
    onPointerDown(_args: PointerEventData) {
        console.log("PointerDown");

        if (!this.eventName) return;

        // Check if google analytics is installed globally. If not make sure it's included (see https://developers.google.com/analytics/devguides/collection/ga4/events?client_type=gtag#add_events_to_your_javascript) 
        if ("gtag" in globalThis) {
            // Call the google analytics function to register your custom event
            globalThis.gtag('event', this.eventName, {
                'app_name': 'myAppName',
                'screen_name': 'Home'
            });
        }
        else console.warn("Google analytics is not installed");

    }
}

If you don’t get the “PointerDown” log in the browser console (F12) then adding a ObjectRaycaster to your scene root or the object that you added the script to should help.

Here are some docs for Unity devs starting with Needle that might help you further along :slight_smile: Scripting Introduction for Unity Developers | Needle Engine Documentation It’s very similar to C#

Thanks, I finally got it, now I need to create a project on google analytics and connect everything. I’ll get back to you next week to let you know if I was able to get it set up.

2 Likes

Alright great. Btw we now have a public roadmap, feel free to vote on features that you’re most interested in:

https://cloud.needle.tools/roadmap

2 Likes

Out of curiosity, did you get to set it up?

Yes I am. I couldn’t get it to work on Netlify and Glitch I couldn’t get Google Analytics installed on the website, but I did get it to work on Github pages.

1 Like

Strange – but glad you figured something out that works for you!