Get answers quicker by searching

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

Automatic tagging for Google Analytics
  • Vote Up0Vote Down cawecoycawecoy
    Posts: 6Sendy user

    I wrote a solution for an automatic tagging for Google Analytics and it is working on Sendy 1.1.7.3.

    How does it work

    Everytime the user clicks on a link in your newsletter, he first goes to your_sendy_installation_folder/l.php and then Sendy finally redirects him to the link he wants to access. My solution just append the Google Analytics query string to this link URL the user is redirected to.

    Installation

    Just copy & paste the code bellow right before the header("Location: $link"); in the end of the your_sendy_installation_folder/l.php file.

    //Get the campaign title from database
    $q6 = 'SELECT title FROM campaigns WHERE id = '.$campaign_id;
    $r6 = mysqli_query($mysqli, $q6);
    if ($r6)
    {
        $row = mysqli_fetch_array($r6);
        $title = $row['title'];
    
        if (preg_match('/\\?[^"]/', $link)) {
            //If the URL has other queries already, then append the Google Analytics query starting with &
            $link .= '&utm_source=Sendy&utm_campaign='.urlencode($title.'&utm_medium=email';
        } else {
           //If not, so these are its first URL query, then append them starting with ?
            $link .= '?utm_source=Sendy&utm_campaign='.$title.'&utm_medium=email';
        }
    }
    

    That's it. Cheers!

  • 4 Comments
  • Vote Up0Vote Down BenBen
    Posts: 3,421Sendy support

    Thanks for sharing this, Cawe. :)

  • Vote Up0Vote Down cawecoycawecoy
    Posts: 6Sendy user

    You are welcome.

    My code has got an error, let me fix it. I forgot to close parenthesis here

    $link .= '&utm_source=Sendy&utm_campaign='.urlencode($title.'&utm_medium=email';
    

    I also forgot to URLENCODE() the title here

    $link .= '?utm_source=Sendy&utm_campaign='.$title.'&utm_medium=email';
    

    The final right code is the following:

    //Get the campaign title from database
    $q6 = 'SELECT title FROM campaigns WHERE id = '.$campaign_id;
    $r6 = mysqli_query($mysqli, $q6);
    if ($r6)
    {
        $row = mysqli_fetch_array($r6);
        $title = $row['title'];
    
        if (preg_match('/\\?[^"]/', $link)) {
            //If the URL has other queries already, then append the Google Analytics query starting with &
            $link .= '&utm_source=Sendy&utm_campaign='.urlencode($title).'&utm_medium=email';
        } else {
           //If not, so these are its first URL query, then append them starting with ?
            $link .= '?utm_source=Sendy&utm_campaign='.urlencode($title).'&utm_medium=email';
        }
    }
    
  • Vote Up0Vote Down mackaaijmackaaij
    Posts: 11Sendy user

    The code seems to take an empty result into account with if ($r6), but did trigger on an autoresponder in my setup. This resulted in an empty $title but still the UTM parameters where appended. So I think this enhancement is needed:

    //Get the campaign title from database
    if($ares_emails_id=='') //if link does not belong to an autoresponder campaign
    {
        $q6 = 'SELECT title FROM campaigns WHERE id = '.$campaign_id;
    }
    else
    {
        $q6 = 'SELECT title FROM ares_emails WHERE id = '.$campaign_id;
    }
    $r6 = mysqli_query($mysqli, $q6);
    

    In case of an autoresponder, query the table ares_emails for $title instead of the table campaign.

    I have little experience with PHP but as l.php already does a check on autoresponder with: if($ares_emails_id=='') I though I'd extend the code. However, it would be better to have it integrated in Sendy :)

    The whole addition would be:

    //Get the campaign title from database
    if($ares_emails_id=='') //if link does not belong to an autoresponder campaign
    {
        $q6 = 'SELECT title FROM campaigns WHERE id = '.$campaign_id;
    }
    else
    {
        $q6 = 'SELECT title FROM ares_emails WHERE id = '.$campaign_id;
    }
    $r6 = mysqli_query($mysqli, $q6);
    if ($r6)
    {
        $row = mysqli_fetch_array($r6);
        $title = $row['title'];
    
        if (preg_match('/\\?[^"]/', $link)) {
            //If the URL has other queries already, then append the Google Analytics query starting with &
            $link .= '&utm_source=Sendy&utm_campaign='.urlencode($title).'&utm_medium=email';
        } else {
           //If not, so these are its first URL query, then append them starting with ?
            $link .= '?utm_source=Sendy&utm_campaign='.urlencode($title).'&utm_medium=email';
        }
    }
    
  • Vote Up0Vote Down BenBen
    Posts: 3,421Sendy support

    Sendy version 2.0 and up now supports Google Analytics natively. Custom modification of code is no longer needed.

    Best regards,
    Ben

This discussion has been closed.
All Discussions