jazzband / django-analytical

Analytics services for Django projects
MIT License
1.2k stars 166 forks source link

Ability to pass FACEBOOK_PIXEL_ID directly in template tag #196

Open ak4zh opened 2 years ago

ak4zh commented 2 years ago

I have one django app hosted with multiple domains and sub-domains, and have different FACEBOOK_PIXEL_ID for all domains. For I ues request.get_host to deside which css etc to serve for the current request.

Is it possible to allow FACEBOOK_PIXEL_ID to be passed in the tag itself {% facebook_pixel_head FACEBOOK_PIXEL_ID %}

bittner commented 1 year ago

(Sorry that this answer comes late!)

How do you currently set the FACEBOOK_PIXEL_ID in your application configuration? Wouldn't a simple if-then-else in your project settings module do the trick? e.g.

SITE_PIXELS = {
    "one.example.com": "123",
    "two.example.com": "456",
    "three.example.com": "789",
}
FACEBOOK_PIXEL_ID = SITE_PIXELS.get(request.get_host())

Apparently, the trick here is to get access to the request object. If that doesn't prove to be possible as some suggest you may set FACEBOOK_PIXEL_ID dynamically before the template gets processed, e.g. inside of a context processor you write yourself.

It may be worth to document such an approach in our documentation. Not sure if it makes sense to change the implementation of the template tag. If you think it is, I would suggest to try to find a generic approach for all template tags. (That said, PRs welcome!)