Implementing Conversion Pixel (Javascript) With Workers?

Hi,

Can someone please explain if it is possible to inject a conversion tracking pixel using workers?
If so, how?
What is the exact code to be used in my worker for the following script:

Please help, looking forward to the code that will inject this in the html right after tag…
Kind Regards,
Al

< script type=“text/javascript” src=“https://ad.network.com/tag_gen.js” data-goal=“007” >< /script >

Hi,

you’d use HTMLRewriter · Cloudflare Workers docs to select the tag you want to insert your js script (i.e. body) and use the append method. From docs:

append(content: Content, contentOptions?: ContentOptions): Element: Inserts content right before the end tag of the element.

Hope that helps.

Is using a worker a requirement for that?

A lesser known option but probably easier in your case would be to use a Cloudflare App,
e.g.: Cloudflare Apps

1 Like