locked
Trying to use Google Analytics in HTTP Module! RRS feed

  • Question

  • User262377158 posted

     Hi,

     I have a http module which we use to track the customers who open up our newsletters we send out to them, its very complicated to explain, but basically followed this tutorial - http://www.aspnetemail.com/samples/emailtracker/default.aspx what is does is hides a key inside the image url on the email we send out and download this image using the http module and pass the key so we log (in a db) the user who read it. Clever huh? Anyway this is not the reason for the post - I'm trying to add Google analytics so we can track it further (I'm not too sure how much info I get out of this) but I'm having problems implementing it - heres the code I have already for the http module -

    I have the google analytics code in a variable and trying to use the RegisterClientScriptBlock to run this javascript, am I doing the impossible? There are no errors shown (as I am just opening a email and hoping the http module returns a error) Does anybody know how to implement this?

    using System;
    using System.Text.RegularExpressions;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Web.Configuration;
    using System.Web.UI;
    using System.Web;

    namespace ImageTracker
    {
        public class TrackRequest1 : IHttpModule   
        {
            private string pattern = @"/images/(?<key>.*)\.aspx";
            private string logoFile = "~/images/theRealLogo.gif";
            private string dbConnectionString = "server=.\\SQLEXPRESS;Initial Catalog=newsletter;Integrated Security=SSPI";

            public const string GOOGLE_ANALYTICS_JAVASCRIPT = @" <script type=""text/javascript""> /* Google TRACKING CODE */ var gaJsHost = ((""https:"" == document.location.protocol) ? ""https://ssl."" : ""http://www.""); document.write(unescape(""%3Cscript src='"" + gaJsHost + ""google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"")); </script> <script type=""text/javascript""> var pageTracker = _gat._getTracker(""UA-XXXXXXX-XX""); pageTracker._initData(); pageTracker._trackPageview(); /* Google TRACKING CODE */ </script> ";

            public ClientScriptManager ClientScript;

            public TrackRequest1()
            {

            }

            public void Dispose()
            {
            }

            public void Init(System.Web.HttpApplication Appl)
            {
                Appl.BeginRequest+=new System.EventHandler(GetImage_BeginRequest);
            }


            public void GetImage_BeginRequest(object sender, System.EventArgs args)
            {
                //cast the sender to a HttpApplication object
                System.Web.HttpApplication application =(System.Web.HttpApplication)sender;

                string url = application.Request.Path; //get the url path

                //create the regex to match for becon images
                Regex r =new Regex( pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase );
                if( r.IsMatch( url ) )
                {
                    MatchCollection mc =  r.Matches( url );
                    if( ( mc != null ) && ( mc.Count > 0 ) )
                    {
                        string key = ( mc[ 0 ].Groups[ "key" ].Value );
                        SaveToDB( key );
                    }

                    //now send the image to the client
                    application.Response.ContentType = "image/gif";
                    application.Response.WriteFile(  application.Request.MapPath( logoFile ) );

                    application.Context.Response.Write("before <br>");

                    if (!ClientScript.IsClientScriptBlockRegistered("GoogleAnalytics"))
                    {
                        ClientScript.RegisterClientScriptBlock(ClientScript.GetType(), "GoogleAnalytics", GOOGLE_ANALYTICS_JAVASCRIPT, true);
                       
                        application.Context.Response.Write("after <br>");
                    }
                                   
                    application.Response.End();
                }

            }


            private void SaveToDB( string key )
            {
                if( ( key==null) || ( key.Trim().Length == 0 ) )
                    return;

                string sqlText = string.Format("INSERT INTO EmailTracker ( emailKey ) VALUES ( '{0}' )", key.Replace("'", "''"));
                SqlCommand cmd = new SqlCommand( sqlText, new SqlConnection( dbConnectionString ) );
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();

            }
       
        }

     

    Cheers for any suggestions!

     

     

    Tuesday, January 6, 2009 4:20 AM

Answers

  • User262377158 posted
    Hi Thanks for all your comments, I wasn't sure if this was possible but I think it will be best to use google email tracking and can track this inside a email campaign - found a good tutorial for anybody who may be interested - http://www.epikone.com/blog/2008/11/04/email-tracking-with-google-analytics/
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 9, 2009 4:05 AM

All replies

  • User1290761991 posted

    I'm not sure if I'm tracking your question or not, but I don't think you can emit markup after setting the header's content type to an image/gif.  I suspect the markup is dropped or consired to be extraneous bits of the image...

     

    Tuesday, January 6, 2009 9:52 PM
  • User1566012831 posted

    The script needs to be emitted as part of a web page, not an image.

    If the user is clicking on a link, as opposed to viewing an email with an embedded image, then you could first return a short page with the analytics code, wrapped by bare-bones HTML, and then have the page redirect to the image.  If you used script to do the redirect, that would help ensure that the analytics code had already been processed (as opposed to web crawlers, which generally don't execute script).

     

    Tuesday, January 6, 2009 10:04 PM
  • User1290761991 posted

    Looking at your handler as you have it now, it appears as though it is used to parse an ID off of the request, log it to a database, and then send a file to the output stream after setting the content type to that of an image.  IMO, because the content type is that of an image your markup is going to be ignored.  Images to not process or execute script tags.  If you want to emit tracking code when rendering an image, I do not think you can do it with a call to a resource that is expected to return an image.

    As far as a user clicking a link goes, if you have them clicking a link, you don't need the page in the middle, just embed the key on the URL they're clicking on.  If you must use the page in the middle, because of the analytics, I don't see a problem.  You simply put googles code on a page that has a client side redirect to the page with the image.  I don't see the handler playing a part in that scenario.

    Tuesday, January 6, 2009 10:47 PM
  • User-1936035690 posted
    Hi,
    Rick is correct. You are trying to emit text (BR tags) inside of image data. You can't mix and match, and get the disired result.

    Cheers!
    Dave
    Wednesday, January 7, 2009 3:29 PM
  • User1290761991 posted
    I was looking at some posts tonight and saw the author of this site answering questions. Does anyone recognize this code? http://www.aspnetemail.com/samples/emailtracker/default.aspx I would recommend following up with the author of your sample code on your question. If you've purchased their control, I'm sure you're receive priority support, otherwise there seems to be a free online forum available. I bet you'll find more on topic specialization there... http://www.advancedintellect.com/forum.aspx?forum=1
    Thursday, January 8, 2009 10:26 PM
  • User262377158 posted
    Hi Thanks for all your comments, I wasn't sure if this was possible but I think it will be best to use google email tracking and can track this inside a email campaign - found a good tutorial for anybody who may be interested - http://www.epikone.com/blog/2008/11/04/email-tracking-with-google-analytics/
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 9, 2009 4:05 AM