I am trying to do tracking pixel implementation where tracking pixel is being loaded from css, not js - body:after { background-image: url(url-to-tracking-pixel); }
. While it works correctly in all other browsers, chrome keeps caching that tracking pixel. It only occurs when css file with code is also cached. How to prevent this so that resource in css file would not be cached in chrome?
1 Answers
Put a ?=datemodstamp
at the end of any URL to prevent caching. The mod stamp would be a date modified timestamp of the file you are referencing. You'll have to echo this with a script.
If you are already referencing a file with ?a=b
and so on, just add &=datemodstamp
.
P.S. And I'm pretty sure Chrome caches everything.
EDIT:
This code won't work because nothing's changing. All that's wanted is for the image to be loaded from the server every time.
The solution?
Use an inline image with a current date stamp query.
<img src="tracking.png?=currentTimeStamp" style="position: absolute; top: -1000px; left: -1000px;"/>
It's basically the same idea as before.
I'm not setting display to none because the browser might not download it. So I'm just hiding it with a negative position.
Have you thought of doing <link rel="stylesheet" href=...
instead of doing an image?

- 1,226
- 8
- 17
-
Thanks for fast response! However, i can't use timestamp you suggested as i would like that css file to be cached. I need that browser would redownload only the link in css not entire css. I can't understand why this behaviour occurs only in chrome, not in other browsers, where cacheability can be modified with http headers. – Osvaldas Dec 02 '13 at 14:51
-
@Osvaldas My bad: you wouldn't be able to use this because your image and stylesheet aren't changing, so no mod date would change. Let me edit my answer. – sheng Dec 02 '13 at 14:58
-
@Osvaldas check the improved answer. :) – sheng Dec 02 '13 at 15:08
-
Thanks for another solution, but i can't use html also as i have to call for a tracking pixel from css - can't use javascript to insert html. I am aware that most tracking systems rely on img elements. – Osvaldas Dec 02 '13 at 15:09
-
@Osvaldas can't you insert the tracking pixel with server-side code? – sheng Dec 02 '13 at 15:11
-
No, there is only css file served to other applications. – Osvaldas Dec 02 '13 at 15:13
-
@Osvaldas here's what you can do: the CSS file can really be a server-side script which does tracking on its own. The script can still print out your CSS file (by reading a file) and be tracking it at the same time. – sheng Dec 02 '13 at 15:15
-
But this would require to disable caching for that css file, what i would like to avoid. – Osvaldas Dec 02 '13 at 15:34
-
@Osvaldas aw man, sorry! I forgot about that! Is this some sort of CSS library where there are no other options? If so, I'm afraid I'm out of ideas. :( – sheng Dec 02 '13 at 16:51