UTF16 encoding of plugin string breaking google analytics?

I strongly suspect now that this:

Is actually a tutor-specific issue related to it seemingly doing UTF-16 encoding of the “-” character (from ASCII 0x2D to UTF-16 “\u002D”). You can see in the below hexdump that it definitely starts off as an ASCII 0x2D. But then as show in the above open edx forum posts, it ends up in javascript as \u002D, which causes google analytics to not work.)

Is there any way to alter this encoding behavior?


> hexdump -C $(tutor plugins printroot)/GoogleAnalytics.yml
> 00000000  6e 61 6d 65 3a 20 67 6f  6f 67 6c 65 61 6e 61 6c  |name: googleanal|
> 00000010  79 74 69 63 73 0a 76 65  72 73 69 6f 6e 3a 20 30  |ytics.version: 0|
> 00000020  2e 31 2e 30 0a 70 61 74  63 68 65 73 3a 0a 20 20  |.1.0.patches:.  |
> 00000030  6f 70 65 6e 65 64 78 2d  63 6f 6d 6d 6f 6e 2d 73  |openedx-common-s|
> 00000040  65 74 74 69 6e 67 73 3a  20 7c 0a 20 20 20 20 23  |ettings: |.    #|
> 00000050  20 67 6f 6f 67 6c 65 61  6e 61 6c 79 74 69 63 73  | googleanalytics|
> 00000060  20 73 70 65 63 69 61 6c  20 73 65 74 74 69 6e 67  | special setting|
> 00000070  73 0a 20 20 20 20 47 4f  4f 47 4c 45 5f 41 4e 41  |s.    GOOGLE_ANA|
> 00000080  4c 59 54 49 43 53 5f 41  43 43 4f 55 4e 54 20 3d  |LYTICS_ACCOUNT =|
> 00000090  20 22 55 41 2d 32 30 31  34 34 33 33 30 36 22 0a  | "UA-201443306".|
> 000000a0  20 20 20 20 47 4f 4f 47  4c 45 5f 41 4e 41 4c 59  |    GOOGLE_ANALY|
> 000000b0  54 49 43 53 5f 54 52 41  43 4b 49 4e 47 5f 49 44  |TICS_TRACKING_ID|
> 000000c0  20 3d 20 22 55 41 2d 32  30 31 34 34 33 33 30 36  | = "UA-201443306|
> 000000d0  2d 31 22 0a                                       |-1".|

@oedx

Which version are you using ?
I tested integrating Google analytics in Koa and it’s working fine.

I’m using Koa & Tutor 11.3.1

Hi @oedx
Can you verify the plugin content (specifically whether account and ID are enclosed in double quotes) ?

name: googleanalytics
version: 0.1.0
patches:
  openedx-common-settings: |
    # googleanalytics special settings
    GOOGLE_ANALYTICS_ACCOUNT = "UA-xxxxxxxxx-x"
    GOOGLE_ANALYTICS_TRACKING_ID = "UA-xxxxxxxxx-x"

Yes, you can see in the above hexdump that the double quotes are used, and correspond to hex 0x22 which is ASCII " (I was worried for a second when you said that, that maybe a unicode “curly quote” had been inserted, but it doesn’t look like it.)

Non-hexdump form is:

name: googleanalytics
version: 0.1.0
patches:
  openedx-common-settings: |
    # googleanalytics special settings
    GOOGLE_ANALYTICS_ACCOUNT = "UA-201443306"
    GOOGLE_ANALYTICS_TRACKING_ID = "UA-201443306-1"

(Also, can you look at the page source for your page, and let me know if you also see
_gaq.push(['_setAccount', 'UA\u002D201443306']);
i.e. with the encoded -, or if it just shows up as a -?)

@oedx I managed to reproduce your issue. The Google analytics account and tracking ID are included in the main.html template with the following piece of code:

<% ga_acct = static.get_value("GOOGLE_ANALYTICS_ACCOUNT", settings.GOOGLE_ANALYTICS_ACCOUNT) %>
...
_gaq.push(['_setAccount', '${ga_acct | n, js_escaped_string}']);

js_escaped_string is basically a call to Django’s own escapejs function. We can verify that this is the function that translates the dash “-” into " “\u002D”:

$ python -c "from django.utils.html import escapejs; print(escapejs('a-b'))"
a\u002Db

Thus:

  1. This is not Tutor-specific: this is an upstream bug that should be reported to Open edX.
  2. I believe edX themselves did not discover this issue because they are no longer using Google Analytics.
  3. I have no idea why this could occur in Lilac, but not in Koa. As far as I understand, this issue dates back from 2017: Style LMS header, footer and nav for Bootstrap. · edx/edx-platform@7ea3b01 · GitHub
  4. I attempted to solve the issue by marking the setting as a safe string, but it didn’t work:
from django.utils.safestring import SafeText
GOOGLE_ANALYTICS_ACCOUNT = SafeText("UA-201443306")

What I did is that I opened an upstream PR: fix: Google Analytics tracking in LMS by regisb · Pull Request #28634 · edx/edx-platform · GitHub
This bug will be tracked here: Google Analytics activation is broken (and the corresponding Tutor docs as well) · Issue #483 · overhangio/tutor · GitHub

@oedx Tim McCormack (from edX) commented on my PR (here) that the connection to Google Analytics should still work despite the replacement of “-” by “\u002D”. Can you confirm? If this is true there is no need for my PR.

@oedx could you please reply to my comment above? It is slightly embarassing for me to have opened a PR which does not address an actual bug…