Google Analytics in RenPy games does not work

In this forum we discuss the future of Ren'Py, both bug fixes and longer-term development. Pre-releases are announced and discussed here.
Post Reply
Message
Author
KaitoSi11
Newbie
Posts: 2
Joined: Sun Sep 08, 2019 9:55 am
Contact:

Google Analytics in RenPy games does not work

#1 Post by KaitoSi11 »

Hey mans, can someone say why this function does not work?
https://patreon.renpy.org/analytics.html

I did everything strictly according to the guide, but nothing is updated on the Google Analytics account. What could be the problem? Is it possible that I missed something?

Code: Select all

# This needs to be set to true to enable analytics.
default persistent.analytics = None

# Initialize this to a unique cid.
default persistent.analytics_cid = None

init -10 python in analytics:

    # The google analytics tracking ID.  If not set, no tracking will occur.
    tracking_id = None

    from store import persistent, config
    import uuid
    import threading
    import urllib
    import urllib2
    import ssl

    # Create a cid if needed.
    if persistent.analytics_cid is None:
        persistent.analytics_cid = str(uuid.uuid4())

    # The queue of events that are going to be called. This should not
    # change, as we don't want this to be saved.
    queue = [ ]

    def enqueue(**kwargs):
        if not persistent.analytics:
            return

        if not tracking_id:
            return

        kwargs["v"] = 1
        kwargs["tid"] = tracking_id
        kwargs["cid"] = persistent.analytics_cid

        queue.append(urllib.urlencode(kwargs))

    def event(category, action, label=None, value=None):
        """
        `category`
            The category of the event. A string.

        `action`
            The action that occurred. A string.

        `label`
            An optional label associated with the event. A string.

        `value`
            If given, a numeric value associated with the ending. A number.

        """

        kwargs = dict(
            t="event",
            ec=category,
            ea=action)

        if label is not None:
            kwargs["el"] = label
        if value is not None:
            kwargs["ev"] = value

        enqueue(**kwargs)

    def post_events(q):
        """
        This is called in a thread from the interact callback, and is responsible for
        actually posting events to Google Analytics.
        """

        data = "\r\n".join(q) + "\r\n"

        context = ssl._create_unverified_context()

        try:
            urllib2.urlopen("https://www.google-analytics.com/batch", data, timeout=10, context=context)
        except:
            if config.developer:
                traceback.print_exc()


    def interact_callback():
        """
        This is called once per interaction, and then
        """

        q = queue[:]
        queue[:] = [ ]

        if not persistent.analytics:
            return

        t = threading.Thread(target=post_events, args=(q,))
        t.daemon = True
        t.start()


    config.interact_callbacks.append(interact_callback)
I tried already and move away from the guide, writing down the key in the analytics.rpy file, but still nothing works.

DragoonHP
Miko-Class Veteran
Posts: 758
Joined: Tue Jun 22, 2010 12:54 am
Completed: Christmas
IRC Nick: DragoonHP
Location: Zion Island, Solario
Contact:

Re: Google Analytics in RenPy games does not work

#2 Post by DragoonHP »

It takes a day to update the values on the dashboard.

Derrizah
Newbie
Posts: 3
Joined: Tue Feb 04, 2020 1:36 pm
Github: Derrizah
Contact:

Re: Google Analytics in RenPy games does not work

#3 Post by Derrizah »

Having the same issue. I'm pretty sure I am missing something but no luck finding what. Put tracking ID, added label callback function, played through the game and nothing. Realtime page didn't show anything so I've waited but dashboard does not show anything either.

unregistered
Newbie
Posts: 17
Joined: Tue Apr 21, 2020 5:18 am
Contact:

Re: Google Analytics in RenPy games does not work

#4 Post by unregistered »

I had problems with the Google Analytics solution at first, too. Then I realized that I was using the wrong Google Analytics version. I thought I might leave a note here for anyone having a similar issue:

It's important you use the "Universal Analytics" version with the identifiers starting with UA-. If yours start with G-, you're in the wrong version (presumably 4). You can register an UA property within the "advanced options" when adding a new property. I also made sure to untick all boxes like in Pytom's tutorial in my account settings and I activated the "allow to overwrite GCLID-tags" option and the "user analysis" option in the property settings (don't whether that's really necessary)

After putting the UA identifier in the code and adding some events, playing myself as a new user appeared in the realtime reports. (Be aware that the realtime report only shows your activity if you're actively playing. To see my former events I needed to select the option "events (last 30 minutes" in the realtime report.)

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]