Bod has asked for the wisdom of the Perl Monks concerning the following question:
I'm looking for a neat way to create a 1x1 transparent PNG to track opening emails. The ways I have been doing it seem overly clumsy.
The code we use in our main CRM reads an image file and outputs it. This is c2012 code and my coding has improved considerably since then!
$file='incl/1x1transparent.png'; print "Content-type: image/png\n"; print "Set-Cookie: abc=xxx; SameSite=none; Max-Age=315576000\n" if $us +er; print "\n"; open IMG, $file; binmode IMG; while (sysread(IMG, $buffer,640)) { print $buffer; } close IMG; exit 0;
I wanted to avoid making an IO call to the filesystem to read in such a small file. My first attempt was to Base64 encode the image and serve that as text but that didn't work...
So I have come up with this solution:
print "Content-type: image/png\n\n"; binmode STDOUT; my $image = GD::Image->new(1, 1); my $white = $image->colorAllocate(255,255,255); $image->transparent($white); print $image->png; exit;
This is better but it seems a bit of overkill to use GD to do this.
Can you suggest a more elegant solution?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Create email tracking image
by afoken (Chancellor) on Mar 18, 2023 at 12:16 UTC | |
Can you suggest a more elegant solution? 1. Don't. A sane mail client won't load remote images, so tracking won't work. 2. Don't generate the same constant image over and over again. Use any tool you like to create the transparent pixel image as GIF or PNG once. I used Google ("transparent pixel gif") and found this page. It lists the resulting binaries conveniently as base64 strings. For a transparent pixel in GIF format, it is R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==, in PNG, it is iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVQYV2NgYAAAAAMAAWgmWQ0AAAAASUVORK5CYII=. Now you can use MIME::Base64 or similar to convert that to a binary:
Of course, you could even get rid of MIME::Base64 by recoding that to hex, and use pack to write the binary without any extra modules: Like this:
You could even get rid of pack:
Et voilą:
(Note: That assumes an ASCII-based system. EBCDIC probably won't work with that constant.) And there is more: You can omit binmode on Unix, as Unix is 8-bit-clean. On DOS-based systems (DOS, OS/2, Windows), binmode prevents converting \n = \012 = 0x0A to \r\n = \015\012 = 0x0D, 0x0A. THIS GIF luckily contains no \n = 0x0A, so it does not matter if \n is converted or not. So for this constant, you can omit binmode also on DOS, OS/2, Windows:
Yipp, a single line of code works on Unix, DOS, OS/2, and Windows. Alexander
-- Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-) | [reply] [d/l] [select] |
by Bod (Parson) on Mar 21, 2023 at 22:34 UTC | |
Don't. A sane mail client won't load remote images, so tracking won't work There is the difference between a programmer and a marketer! We get between 30% and 50% reported open rate from most emails on their first send1. So, at least 30% of our email lists have images turned on. Whilst it may seem an increase in privacy to block remote images, it actually means that the end user receives far less relevant content. A typical email campaign of ours splits the audience into four groups. Our email campaigns are quite sophisticated with some campaigns having over 100 different emails and variations for different people at different times. Most of the content is educational material designed to help our prospects, build our credibility and remind them that we exist. This level of sophistication and relevance would be limited without tracking images enabled as we would be relying solely on clicks and other triggers. 1 Typically the first email in a campaign will be resent up to 5 times with a different subject to people who have not yet opened it 2. Don't generate the same constant image over and over again. Yeah - that was exactly the point of the question... And there is more: You can omit binmode on Unix, as Unix is 8-bit-clean. Thank you kindly That is exactly the kind of solution I was looking for and having a mental block over! | [reply] |
by bliako (Abbot) on Mar 22, 2023 at 13:32 UTC | |
If I was the target I would prefer a newsletter to contain weblinks for further reading after an initial title/introduction in the email body. This way you would be getting multi-dimensional data on each genuinely interested user. Weather they opened it or not it does not matter. Have they expressed interest in visiting the stories/products? That's what matters.Then keep changing the stories to see if you get any heartbeat. Deleting the newsletter unread, straight from the queue perhaps it's because they have the impression it is of no interest whatsoever. In which case I find it easier to unsubscribe and get rid with this hassle. Why would I delete email unread?: Tacky titles, past experience as being useless or with aggressive marketing, marketing lingo, excessive company logos, headers, tons of legal footer, idiots. | [reply] |
Re: Create email tracking image
by cavac (Prior) on Mar 20, 2023 at 16:07 UTC | |
There are many problems with those so called tracking bugs First of all, most email clients won't load remote content unless told to do so by the user (which is a per-message thing). Basically, you are 20 years too late. Secondly, spam filters look out for exactly those things and are very likely to flag your message as spam when they detect a remotely loaded 1 pixel image. In the course of mail processing and spam filtering, the mail system may already load the image (either for checking or for embedding in the message). Last but not least: Tracking bugs probably violate various privacy laws all around the world. The EU has the GDPR, and the UK has replaced that (after Brexit) with it's own version "UK-GDPR" (pretty much a word-for-word copy AFAIK). As you stated, you want to track user behaviour. This requires explicit consent by the end user! A GDPR violation can get expensive. I am not a lawyer, but this is what i can see: A user can state non-material damages (suffered stress due to your privacy violation or something similar) and claim compensation. GDPR Article 82 subsection 1:
Tracking a user seems to (at least) violate the spirit of Article 25 "Data protection by design and default" of the GDPR, since there is no technical requirement to track the delivery of an email and no reasonable user expectation that the reading status of emails are tracked. Also, you need the other infrastructure in place to deal with data protection and handling of user requests pertaining to the GDPR (deletion requests, information requests, etc). Violating GDPR can result in administrative fines, see Article 83 subsection 4:
So technically, you could possibly get fined 10 million Euros (or more, depending on the financial success of your company) for adding a tracking mechanism to your outgoing email. Ooof, that doesn't sound like a project worth pursuing.
PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
| [reply] [d/l] [select] |
by LanX (Saint) on Mar 20, 2023 at 16:50 UTC | |
I just stumbled over a duckduckgo mail forwarding service promising to cleanse emails from tracking stuff. Apparently is (inter)national legislation not (yet) doing enough to force providers to automatically do so.
Cheers Rolf | [reply] |
by cavac (Prior) on Mar 20, 2023 at 17:06 UTC | |
I seems to me it's a grey zone because too many of the big players are still profiting from user profiling. Possibly. But it's one of those grey zones that are probably stricter for small companies than they are for bigger ones. Microsoft and Google can easily throw a few million bucks in the direction of their lawyers to make such problems magically go away (and earn those millions back in a matter of hours). I somehow doubt that Bod has the same options...
PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
| [reply] |
by LanX (Saint) on Mar 20, 2023 at 23:40 UTC | |
by Bod (Parson) on Mar 21, 2023 at 22:45 UTC | |
Basically, you are 20 years too late I know of no CRM or mail software that uses any other method... For example, we use Send In Blue for some transactional emails and use their email tracking. They add something like this to their emails: (tracking code changed) Having a user click on a link is a more precise trigger of action as users and mail software can affect opening data but we still need to know who has definitely opened an email to be able to personalise relevant content. The data we collect in email processing is not personal data as defined by GDPR and our data processing systems are registered with the Information Commissioners Office as required here in the UK. | [reply] [d/l] |
Re: Create email tracking image
by LanX (Saint) on Mar 18, 2023 at 16:31 UTC | |
One of the reasons are spammers who need feedback about their success. For instance a server might decide to pre-fetch all images before the mail is read and include them as inlined base 64. AFAIR is Gmail doing this.
UpdatesCheers Rolf | [reply] |
Re: Create email tracking image
by harangzsolt33 (Deacon) on Mar 19, 2023 at 02:59 UTC | |
| [reply] [d/l] |
Re: Create email tracking image
by harangzsolt33 (Deacon) on Mar 19, 2023 at 03:15 UTC | |
If I wanted to know for sure if someone read my email, then I would write several paragraphs but I would only include part of the first sentence in the email to make the reader curious. Then I would include a link to click where they can read the REST of the email. If somebody clicks the link, then you can serve the page using a perl script, and you would automatically get an email-read notification that way! ;) | [reply] |
by cavac (Prior) on Mar 20, 2023 at 15:31 UTC | |
because for some reason the https protocol is disabled in my email software I would hazard a guess that OEClassic hasn't been updated to use modern encryption APIs on Windows and the one it tries to use has been disabled by some windows security update.
PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
| [reply] |
ChatGPT's solution (was: Re: Create email tracking image)
by Bod (Parson) on Mar 21, 2023 at 22:56 UTC | |
Just for fun I asked ChatGPT how do do this using the prompt: "What is the best way to create a 1x1 transparent image in Perl for use in email tracking?" It came up with this solution: And then went on to explain how it worked... I prefer the solution put forward by afoken in Re: Create email tracking image | [reply] [d/l] |
by Anonymous Monk on Mar 22, 2023 at 23:45 UTC | |
And then went on to explain how it worked... How very amazing. But what about absolutely irrelevant fact, that image produced is NOT transparent, bloody damn it, but 100% opaque? Should have been either:
or:
What a sad scam. And yeah, I feel so very stupid to have to argue with bot. Bright future ahead. | [reply] [d/l] [select] |
by Anonymous Monk on Mar 23, 2023 at 16:19 UTC | |
Your first example works and makes sense to me because the transparent method is called with an allocated color and this causes it to return the index of the transparent color: 0 instead of -1. You second example works but since the transparent method is not called transparent continues to return -1 which according to the doc means transparency is disabled, so how is it transparent? The answer from gpt looks like it should work like your first example works: the allocated alpha "color" of 127 (transparent) is passed to the transparent method, but instead of returning 0 or -1 or something else, the transparent method now returns 2130706432 which is... two gigabytes? Output: -1 at line 9. 0 at line 11. -1 at line 18. -1 at line 20. -1 at line 27. 2130706432 at line 29. | [reply] [d/l] |
by Anonymous Monk on Mar 23, 2023 at 23:30 UTC | |
by Bod (Parson) on Mar 23, 2023 at 10:40 UTC | |
How very amazing. But what about absolutely irrelevant fact, that image produced is NOT transparent It is very amazing and also far from perfect. But... The AI landscape is moving very fast and already, there are more capable systems. Just like the internet of today could not have been envisioned when the internet arrived in the 1990s (who could have thought about Alexa when we used dial-up?), so AI will improve rapidly and take us to places that we cannot imagine today. What we are seeing now is just the beginning. Being an optimist, I am very excited for the future of this technology and consider it an amazing time to be alive... | [reply] |
by marto (Cardinal) on Mar 23, 2023 at 11:20 UTC | |
by LanX (Saint) on Mar 24, 2023 at 02:03 UTC | |
| |
by Anonymous Monk on Mar 23, 2023 at 11:10 UTC | |
by Anonymous Monk on Mar 22, 2023 at 21:49 UTC | |
I wonder what it thinks you mean by "the best way" when there are so many:
| [reply] [d/l] |
by Bod (Parson) on Mar 23, 2023 at 00:58 UTC | |
You didn't include the generated explaination of how the code works so... Here you go! This code creates a new transparent image with a size of 1x1 pixel using the GD::Image module. It sets the alpha blending to 0 and saves the alpha channel to enable transparency. Then it allocates a transparent color and sets it as the transparent color for the image. Finally, it outputs the image as a PNG. | [reply] |
by misc (Friar) on Mar 27, 2023 at 14:16 UTC | |
it's already
| [reply] [d/l] |
Re: Create email tracking image
by Anonymous Monk on Mar 18, 2023 at 09:58 UTC | |
| [reply] |
by Bod (Parson) on Mar 18, 2023 at 12:17 UTC | |
That doesn't help... | [reply] [d/l] |
by Anonymous Monk on Mar 18, 2023 at 15:02 UTC | |
[reply] |