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?
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
}
}
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.
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");
}
}
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?
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.
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.
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.
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.