Page 1 of 1

Google Analytics in RenPy games does not work

Posted: Fri Sep 13, 2019 10:29 am
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.

Re: Google Analytics in RenPy games does not work

Posted: Sun Dec 22, 2019 8:10 am
by DragoonHP
It takes a day to update the values on the dashboard.

Re: Google Analytics in RenPy games does not work

Posted: Wed Feb 12, 2020 2:00 pm
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.

Re: Google Analytics in RenPy games does not work

Posted: Fri Aug 13, 2021 3:36 pm
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.)