betaacid / expo-analytics

Google Analytics integration for use with React Native apps built on Expo
MIT License
288 stars 63 forks source link

Sending events sets pages as not set in google analytics -> events -> pages #62

Closed BillyCottrell closed 4 years ago

BillyCottrell commented 4 years ago

Sending a regular events is not a problem, but when checking the events in google analytics under pages all events are categorized underneath "not set".

The issue occured when having 2 components (Homescreen with a list and item component), the hit is executed in the homescreen but the event is triggered in/by the item component and for some reason when sending it to ga it loses the page info.

I tried to test this out and move the event to the homescreen which gets called by the item component, but without success.

Screenshot: afbeelding

I have been using React Native with managed expo and made a singleton for the analytics instance.

import { Analytics, PageHit, ScreenHit, Event } from 'expo-analytics';

export default class ExpoAnalyticsManager {

    static instance = null;

    _analytics = null;

    static getAnalytics() {
        if(ExpoAnalyticsManager.instance ==null){
            ExpoAnalyticsManager.instance = new ExpoAnalyticsManager();
            this.instance._analytics = new Analytics("UA-153309176-1");
        }
        return this.instance;
    }

    pageHit(name){
        this._analytics.hit(new PageHit(name)).then(() => console.log("succes " + name)).catch(e => console.log(e.message));
    }

    screenHit(name){
        this._analytics.hit(new ScreenHit(name)).then(() => console.log("succes " + name)).catch(e => console.log(e.message));
    }

    event(category,action,optReclabel=null,optvalue=null){
        this._analytics.event(new Event(category, action, optReclabel, optvalue)).then(() => console.log("Succes Category: " + category + ". Action: " + action + ". Label: " + optReclabel + ". Value: " + optvalue )).catch(e => console.log(e.message));
    }

    addCustomDimension(id,value){
        this._analytics.addCustomDimension(id,value);
    }

    addCustomMetric(id,value){
        this._analytics.addCustomMetric(id,value);
    }

    removeCustomDimension(id){
        this._analytics.removeCustomDimension(id);
    }

    removeCustomMetric(id){
        this._analytics.removeCustomMetric(id);
    }

}

When the button was clicked the event was triggered through following code:

ExpoAnalyticsManager.getAnalytics().event("Like","Like-" + recipes[recipeIndex].name.replace(/\s+/g, '-'), recipes[recipeIndex].name, 1);

So the events get passed without any issue and prints the event in the console correctly.

Is it possible that i have to send a hit when sending an event so it gets linked or is there a different reason to why the page information isn't known?

BillyCottrell commented 4 years ago

Yes but that's a different library as im using this library for my bachelor thesis which is a comparison study of 3 different analytic tools, so i have to use this library.

BillyCottrell commented 4 years ago

I have made changes to the singleton to be able to use this properly. Instead of creating the analytics only once i now create it everytime I use the getAnalytics() method. When I call the method I send the parameters object with it so when sending an event I can send the page information through the param object.

static getAnalytics(params) {
        if(ExpoAnalyticsManager.instance ==null){
            ExpoAnalyticsManager.instance = new ExpoAnalyticsManager();
        }
        this.instance._analytics = new Analytics("UA-153309176-1",params);
        return this.instance;
    }

Example of usage:

ExpoAnalyticsManager.getAnalytics({dt:"Get Ideas"}).event("Like","Like-" + recipes[recipeIndex].name.replace(/\s+/g, '-'), recipes[recipeIndex].name, 1);