umami-software / umami

Umami is a simple, fast, privacy-focused alternative to Google Analytics.
https://umami.is
MIT License
23.12k stars 4.3k forks source link

Use Umami on Google AMP pages #751

Closed nicolasleroy closed 3 years ago

nicolasleroy commented 3 years ago

Hello, Thank you for Umami.

I'm trying to make it work on Google AMP pages, using the POST API and the guide at https://amp.dev/documentation/components/amp-analytics/

Here is the code I'm using:

<amp-analytics id="umami">
    <script type="application/json">
        {       
            "requests": {
                "event": "https://u.my-domain.com/api/collect"
            },
            "extraUrlParams": {
                "payload": {
                    "website": "43c3af03-b54a-4b60-87d4-c599289XXXX",
                    "url": "${sourceUrl}",
                    "referrer": "${documentReferrer}",
                    "hostname": "my-domain.com",
                    "language": "en-US",
                    "screen": "1920x1080"
                }                
            },
            "triggers": {
                "trackPageview": {
                    "on": "visible",
                    "request": "event",
                    "extraUrlParams": {                     
                        "type": "pageview"
                    }
                }
            },
            "transport": {
                "beacon": true,
                "xhrpost": true,
                "image": false,
                "useBody": true
            }
        }
    </script>
</amp-analytics>

This code unfortunately triggers the following error in the browser "Failed to load resource: the server responded with a status of 500 ()". On the server, there is nothing logged in the error log, so I'm left wondering.

Would someone have any clue ?

Thanks

nicolasleroy commented 3 years ago

I have narrowed the issue down.

With Google AMP pages, when using the "amp-analytics" component, the request header Content-Type is set to text/plain. It's done on purpose as explained here: https://github.com/ampproject/amphtml/issues/22167

Umami returns a 500 error code when the request header Content-Type is not set to application/json.

Solution 1: I can write my own proxy to call the Umami API as intended. Solution 2: An adjustment can be made in Umami, in order to support the use case of tracking AMP pages ?

Thanks !