Search code examples
phpcodeigniteremailgoogle-analyticsanalytics

how to send analytics script in mail format


I have an analytics script in my website. It is tracking my products which are ordered. So it's available on the page but I want that script to be mailed as well. Following is the code which fails to include the script with the email.

My_Controller:

        $returnData .= "<script>
    (function(.....);
</script>
<script>
    ga('require', 'ecommerce');
    ga('ecommerce:addTransaction', {
        'id': '',
            .
            .
            .
            .
            .
    ga('ecommerce:send');
</script>";

        $this->send_mail($id,$data);

        return $data;
    }

public function send_mail($id,$data)
    {
        require_once 'phpmailer/PHPMailerAutoload.php';
        $mailto = "[email protected]";
        $name   = "Testing";
        $content= "Result - $data <br>";
        $subject= "Test Mail - $id : ";

        $mail = new PHPMailer;

        $mail->isSendmail();
        $mail->setFrom('[email protected]', 'Tester');           
        $mail->addAddress($mailto, $name);
        $mail->Subject = $subject;
        $mail->msgHTML($content);

        if ($mail->send()) {
            return true;
        }else{
            return false;
        }
    }

Solution

  • Well, first off, you're assigning $returnData, but calling $this->send_mail($id,$data) . I would use the same variable if I were you.

    Secondly, including javascript inside an email will fail in most email clients, as they don't allow javascript to be injected in the email body (for security/privacy reasons).

    This being said, I would use a text file as attachment as it looks cleaner and easier to work with anyway.

    Hope this answers your question!