views:

221

answers:

1

I have a php-apache website on which I am trying to track download conversions using Google Analytics. I want my users to initiate the download and be redirected to a "thank you" page in one click. The way I'm envisioning this is:

The user clicks one of several download buttons which sends them to a generic thankyou.php page, and passes a variable telling that page which file to give them. Thankyou.php contains a header which uses that variable to start a download dialogue.

If there are better ways to do this, I am open to anything. To my bewilderment, I haven't found a good way to do this after several hours of poking around here and on Google.

Many thanks in advance :-)

+1  A: 

Send the file after the ThankYou page has been loaded. You could either use an iframe for this (if this is allowed in your (X)HTML variant), e.g.

<iframe src="download.php?id=123" style="display:none;" />

or a meta refresh, e.g.

<META http-equiv="refresh" content="1;URL=download.php?id=123">

or use JavaScript or whatever is able to call a URL. You could then write something like your download should start automatically in a second. If not please click this link, where the link is href'ed to download.php?id=123 as well.

In download.php you'd just send the regular headers for sending a file and pass it to the client. Check this question's answers to see how.

Also see this related question.

Gordon
In your example then, download.php is just a script that queues a download dialogue? Something like <a href="http://www.ryboe.com/tutorials/php-headers-force-download">this</a>, perhaps? I think I'll have to use meta refresh since my site is xhtml 1.0 strict.
Adam
check my update. you need to read the id for the file, send the appropriate headers and then passthru the file to the client and exit. That's all. No HTML on the download.php page.
Gordon
Thanks so much, Gordon. I think this is just what I needed.
Adam