1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Discuss: Integrating Tracking URLs & Shortening Services

Discussion in 'General Programming Chat' started by judaculla, Aug 17, 2016.

  1. judaculla

    judaculla Jr. VIP Jr. VIP

    Joined:
    Oct 11, 2014
    Messages:
    325
    Likes Received:
    119
    Location:
    USA
    I'm trying to piece together an analytics-type service, which would account for individual user actions, across a varying amount of mediums such as Twitter, FB, email, web, etc...

    Obviously, I realize I can use any sort of native tracking on 3rd party sites.

    Basically what I think I'm looking for, is the following arrangement:

    user link (vanity url) ==>Tracking URL ==> $ URL

    So, ideally, any user would only ever see a shortened url link, and not some ugly 300-character UTM link, but would still trigger tracking data to be pushed to my database. I would then be able to get the tracking data and consolidate it with my user data.

    Currently, I'm imagining using a third party shortener service, and a self-hosted analytics service such as Piwik, which I have some familiarity with.

    I was hoping someone with a little more experience doing such an approach might be kind enough to weigh in, pointing out potential pitfalls and better approaches.
     
  2. Des_cartes

    Des_cartes Junior Member

    Joined:
    Jan 19, 2012
    Messages:
    160
    Likes Received:
    64
    Not sure to understand.

    How is bitly enterprise or rebrandly not doing what you want? Why do you want to add Piwik on to of that? What kind of data are you really trying to get with that setup?
     
  3. judaculla

    judaculla Jr. VIP Jr. VIP

    Joined:
    Oct 11, 2014
    Messages:
    325
    Likes Received:
    119
    Location:
    USA
    They very well may be, that's kind of the feedback I was looking to get. I don't know a whole lot about the options already out there....

    I jump to Piwik for 2 reasons; 1.) I've used it before. 2.) No one else sees my data.

    I'm looking for a means (or service) that keeps record of the tracking parameters I pass through links, ideally one that accommodates custom variables, and makes that data accessible for me to download and associate with local data. The shortening ability is purely aesthetic.

    I'm only familiar with services such as Bit.ly or goo.gl as far as shortening the url, and never really learned how else those services can be utilized. I was hoping someone doing the same thing would share what has been working for them.

    As far as the intended data, I'm physically choosing which users to send offers to on my machine, which allows me to track per-user dynamics. I'm looking to be able to pass that tracking through to the final url where I'm gathering other analytical data ($ site most commonly), and be able to access that data via API to download and associate locally. That way, I'll know that for User 0001 has clicked offers 1, 5, 8, and 14 for a 20% CTR. (among other things) None of these links are going to be on indexed sites, so I don't care if there are multiple redirects before the user gets to their final destination.
     
  4. Des_cartes

    Des_cartes Junior Member

    Joined:
    Jan 19, 2012
    Messages:
    160
    Likes Received:
    64
    Not sure if something like that already exist, so the way I would do it is code it myself.
    Buy a domain name (or use one that you already have)
    Setup a server with an .htaccess like that (that will send all request to index.php)
    Code:
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [QSA,L]
    
    Now let's say you have campaign called "123", you setup a database and set the campaign "123" (make it simple, just the name and the destination url)
    You'll then send a link like that: http://domain.com/123/$ (where "$" is the user id)

    You also need an index.php file like that:
    Code:
    $params = explode('/', $_SERVER['REQUEST_URI']);
    
    if(!empty($params[0]) {
      $pdo = new PDO('mysql:host=localhost;dbname=database', 'user', 'password'); // DB connection
      $stmt = $pdo->prepare("SELECT destination FROM campaing WHERE name = :name");
      $stmt->bindParam(':id', $params[0], PDO::PARAM_STR);
      $destination = $statement->fetch(PDO::FETCH_ASSOC);
      if($destination) {
        $stmt = $pdo->prepare(""); //Update user or insert new row
        $stmt->bindParam(':user_id', $params[1], PDO::PARAM_INT);
        $stmt->execute();
        header('Location: '.$destination);
      }
    }
    
    (Don't use that code, it's just a quick example)

    Since you now have everything in your database you can query it the way you want to get the data locally.
     
  5. judaculla

    judaculla Jr. VIP Jr. VIP

    Joined:
    Oct 11, 2014
    Messages:
    325
    Likes Received:
    119
    Location:
    USA
    Thanks for the reply, that's generally the concept I'm looking for. However, I'm looking to deliver users links that are shortened, like sh.rtly/sdfjj123k—which is what I'm trying to understand the best way to integrate into a system like you mentioned above. That's definitely the approach I'm looking for in the end. I'm trying to understand if I need to have two separate service-type actions taking place—the initial redirect with the shortened url, and the secondary redirect as in your example—or if there is a way to integrate the entire process into a single service. Or, if there is something out there already.

    Keeping things tidy would be a goal, but not a deal breaker.
     
  6. Des_cartes

    Des_cartes Junior Member

    Joined:
    Jan 19, 2012
    Messages:
    160
    Likes Received:
    64
    Well with the solution I gave you there is no need for a shortner, buy a short domain and your link will look like that: http://sh.rtly/sdfjj1/23 (Assuming the campaign name is sdfjj1 and user id is 23)

    Now if you don't want a slash in your url you can set your campaign to always be 4 alphanumeric chars (1,679,616 unique possibilities) then add the user id after that.
    To get the parameters, instead of:
    Code:
    $params = explode('/', $_SERVER['REQUEST_URI']);
    
    you'll use:
    Code:
    $campaign = substr($_SERVER['REQUEST_URI'], 0, 4);
    $user_id = substr($_SERVER['REQUEST_URI'], 5, count_chars($_SERVER['REQUEST_URI']));
    
    and change the rest of the code accordingly.

    Then your url can look like that: http://sh.rtly/abc923 (Again, assuming the campaign name is abc9 and user id is 23)
     
    Last edited: Aug 18, 2016
  7. judaculla

    judaculla Jr. VIP Jr. VIP

    Joined:
    Oct 11, 2014
    Messages:
    325
    Likes Received:
    119
    Location:
    USA
    Awesome, that does seem like it would work great! Thanks for your help!