BuilderIO / partytown

Relocate resource intensive third-party scripts off of the main thread and into a web worker. 🎉
https://partytown.builder.io
MIT License
13k stars 431 forks source link

How can I integrate matomo analytics? #333

Open rrolla opened 1 year ago

rrolla commented 1 year ago

Is your feature request related to a problem? Please describe. I want to use this fantastic idea to offload main thread for matomo analytics

Describe the solution you'd like Help to understand how can I integrate it with code examples, and update forwarding events documentation with advanced cases

Describe alternatives you've considered Point to some similar integration

Additional context JavaScript Tracking Client for matomo https://developer.matomo.org/guides/tracking-javascript-guide

I added scripts


<script>
  partytown = {
    debug: true,
    forward: ["_paq.push"],
    lib: "/~partytown/",
    logCalls: true,
    logGetters: true,
    logSetters: true,
    logImageRequests: true,
    logMainAccess: true,
    logScriptExecution: true,
    logSendBeaconRequests: true,
    logStackTraces: true
  };
</script>

<script>
  window._paq = [];
  _paq.push(["trackPageView"]);
  _paq.push(["enableLinkTracking"]);
  _paq.push(["setTrackerUrl", "https://stats.example.com/matomo.php"]);
  _paq.push(["setSiteId", "14"]);
</script>
<script src="https://stats.example.com/matomo.js" async type="text/partytown"></script>

I also fixed issues with cors, so that mean web worker can fetch my analytics script because there are no more errors about cors

But cannot understand how can I forward push to array with value 'trackPageView' eg:

_paq.push(['trackPageView']);

If I execute manually on console it throws error

  _paq.push(['trackPageView']); 
_paq.push() was used but Matomo tracker was not initialized before the matomo.js file was loaded. Make sure to configure the tracker via _paq.push before loading matomo.js. Alternatively, you can create a tracker via Matomo.addTracker() manually and then use _paq.push but it may not fully work as tracker methods may not be executed in the correct order. 
Array [ "trackPageView" ]

Looks like party town itself loads, but cannot send anything to matomo image

mhoev commented 1 year ago

@rrolla, did you make by chance any progress in this? Thank you!

rrolla commented 1 year ago

@rrolla, did you make by chance any progress in this? Thank you!

No unfortunately no progress on this, I added in old way for now.

slamer59 commented 1 year ago

Hello, I am also interested. If someone could guide us to make it work ?

AlejandroAkbal commented 1 year ago

I'm interested in this, please let me know of a solution

jpmaga commented 1 year ago

I haven't tested anything, but just a wild guess. It could be that since you are forwarding _paq.push, partytown creates window._paq.push as a function to forward the event. But after that you are redefining it (window._paq = [];), so that might be the cause why it is not working. Good luck.

nekochan0122 commented 1 year ago

I'm also facing this issue, any update?

woldtwerk commented 1 year ago

I'm also facing this issue, any update?

got this from matomo:

Currently we do not sent access control headers with our JS files but this is something we are considering doing.

We have had several requests and made it a priority issue.

But I wouldn't get my hopes up. The rest of the support ticket indicates, that they don't really know what partytown is or what the header does. In their opinion csp setting of the site is wrong 🫠

j0Shi82 commented 10 months ago

I have a working showcase that integrates Matomo within Shopify OS 2.0. Maybe it helps:

https://github.com/j0Shi82/shopify-partydawn

gioboa commented 10 months ago

Thanks @j0Shi82 for sharing 👍