Giter Site home page Giter Site logo

yorcreative / laravel-urlshortener Goto Github PK

View Code? Open in Web Editor NEW
90.0 3.0 14.0 112 KB

A Laravel URL Shortener package that provides URL redirects with optionally protected URL password, Hidden UTM parameter tracing, URL expiration, open limits before expiration, ability to set feature activation dates, and click tracking out of the box for your Laravel applications.

License: MIT License

Dockerfile 0.47% PHP 98.02% Blade 1.51%
laravel laravel-package package shortener-service shortener-url url-shortener urls lumen-framework shorten-urls shorturl

laravel-urlshortener's Introduction



Laravel URL Shortener

GitHub license GitHub stars GitHub issues GitHub forks Packagist Downloads PHPUnit

A Laravel URL Shortener package that provides URL redirects with optionally protected URL password, URL expiration, open limits before expiration, ability to set feature activation dates, and click tracking out of the box for your Laravel applications.

Installation

install the package via composer:

composer require yorcreative/laravel-urlshortener

Publish the packages assets.

php artisan vendor:publish --provider="YorCreative\UrlShortener\UrlShortenerServiceProvider"

Run migrations.

php artisan migrate

Upgrade Guides

Upgrading to v2.x from v1.x

Usage

Building Short Urls

/**
* Basic
 */
$url = UrlService::shorten('something-extremely-long.com/even/longer?ref=with&some=thingelselonger')
        ->build(); 
// http(s)://host/prefix/identifier;

/**
* Advanced
 */
$url = UrlService::shorten('something-extremely-long.com/even/longer?ref=with&some=thingelselonger')
        ->withActivation(Carbon::now()->addHour()->timestamp)
        ->withExpiration(Carbon::now()->addDay()->timestamp)
        ->withOpenLimit(2)
        ->withOwnership(Model::find(1))
        ->withPassword('password')
        ->withTracing([
            'utm_id' => 't123',
            'utm_campaign' => 'campaign_name',
            'utm_source' => 'linkedin',
            'utm_medium' => 'social',      
        ])
        ->build();
// http(s)://host/prefix/identifier;

Finding Existing Short Urls

/**
 * Find a Short URL by its identifier 
 */
$shortUrl = UrlService::findByIdentifier('identifier');
// returns instance of ShortUrl Model.


/**
 * Find a Short URL by its hashed signature
 */
$shortUrl = UrlService::findByHash(md5('long_url'));
// returns instance of ShortUrl Model.


/**
 * Find a Short URL by its plain text long url string 
 */
$shortUrl = UrlService::findByPlainText('long_url');
// returns instance of ShortUrl Model. 

/**
 * Find shortUrls by UTM combinations.
 * 
 * Note* This method only accepts the following array fields:
 *  - utm_id
 *  - utm_campaign
 *  - utm_source
 *  - utm_medium
 *  - utm_content
 *  - utm_term
 */
$shortUrlCollection = UrlService::findByUtmCombination([
    'utm_campaign' => 'alpha',
    'utm_source' => 'bravo',
    'utm_medium' => 'testing'
])
// returns an instance of Eloquent Collection of ShortUrl Models.

Getting Click Information

$clicks = ClickService::get()->toArray();

dd($clicks);
[
    'results' => [
        [
            'id' => ...,
            'created_at' => ...,
            'short_url' => [
                'id' => ...,
                'identifier' => ...,
                'hashed' => ...,
                'plain_text' => ...,
                'limit' => ...,
                'tracing' => [
                    'id' => ...,
                    'utm_id' => ...,
                    'utm_source' => ...,
                    'utm_medium' => ...,
                    'utm_campaign' => ...,
                    'utm_content' => ...,
                    'utm_term' => ...,
                ]
                'created_at' => ...,
                'updated_at' => ...
            ],
            'location' => [
                'id' => ...,
                'ip' => ...,
                'countryName' => ...,
                'countryCode' => ...,
                'regionCode' => ...,
                'regionName' => ...,
                'cityName' => ...,
                'zipCode' => ...,
                'isoCode' => ...,
                'postalCode' => ...,
                'latitude' => ...,
                'longitude' => ...,
                'metroCode' => ...,
                'areaCode' => ...,
                'timezone' => ...,
                'created_at' => ...,
                'updated_at' => ...
            ],
            'outcome' => [
                'id' => ...,
                'name' => ...,
                'alias' => ...,
            ],
        ]  
    ],
    'total' => 1
];

Getting Click Information and Filtering on Ownership

$clicks = ClickService::get([
    'ownership' =>  [
        Model::find(1),
        Model::find(2)
    ]        
]);

Filter on Outcome

$clicks = ClickService::get([
    'outcome' => [
        1, // successful_routed
        2, // successful_protected
        3, // failure_password
        4, // failure_expiration
        5  // failure_limit
    ]        
]);

Filter on the Click's YorShortUrl Status

$clicks = ClickService::get([
    'status' => [
        'active',
        'expired',
        'expiring' // within 30 minutes of expiring
    ]        
]);

Filtered on YorShortUrl Identifier(s)

$clicks = ClickService::get([
    'identifier' => [
         'xyz',
         'yxz'
    ]
]);

Filtered Clicks by UTM parameter(s). These Can be filtered together or individually.

$clicks = ClickService::get([
    'utm_id' => [
         'xyz',
         'yxz'
    ],
    'utm_source' => [
         'linkedin',
         'facebook'
    ],
    'utm_medium' => [
         'social'
    ],
    'utm_campaign' => [
         'sponsored',
         'affiliate'
    ],
    'utm_content' => [
         'xyz',
         'yxz'
    ],
    'utm_term' => [
         'marketing+software',
         'short+url'
    ],
]);

Iterate Through Results With Batches

$clicks = ClickService::get([
    'limit' => 500
    'offset' => 1500
]); 
  
$clicks->get('results');
$clicks->get('total');

Putting it all Together

/**
 * Get the successfully routed clicks for all active short urls that are owned by Model IDs 1,2,3 and 4.
 * Set the offset of results by 1500 clicks and limit by the results by 500.
 */
$clicks = ClickService::get([
    'ownership' => Model::whereIn('id', [1,2,3,4])->get()->toArray(),
    'outcome' => [
        3 // successful_routed
    ],
    'status' => [
        'active'
    ],    
    'utm_campaign' => [
        'awareness'
    ],      
    'utm_source' => [
        'github'
    ],
    'limit' => 500
    'offset' => 1500
]);

UTM Support

When creating a Short URL, the following UTM parameters are available to attach to the Short URL for advanced tracking of your Short Urls.

  • utm_id
  • utm_campaign
  • utm_source
  • utm_medium
  • utm_content
  • utm_term

UTM information is hidden in the Short URL identifier and clicks are filterable by UTM parameters.

Testing

composer test

Credits

laravel-urlshortener's People

Contributors

gnixner avatar onurcanalp avatar spekulatius avatar yordadev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

laravel-urlshortener's Issues

Short URL Paywall

Summary:

Provide ability to build short URLs that are protected behind a paywall.

Expectations:

  • Single use access paywall

    • User or Guest pays fee per access
    • Long URL is proxied and is masked for single use
  • Persistent use paywall

    • User or Guest pays once and has perm access.
    • Long URL is proxied and is masked for single use

Ability for multiple domains

Summary:

Provide ability to build short URLs that has multiple domains/prefixes

Use cases:

I develop a SAS project that we would like to incorporate this package into. However, Since we have multiple domains for different things we'd like to be able to specify which domain to use. For example, we want to send a short link for a specific feature set that is under one domain or subdomain but also want to provide shortlinks for another domain under the same sourecode and configuration setup. The current code base as it stands won't allow me to create these shortlinks easily without some string replacement which I would prefer to avoid.

Expectations:

  • Ability to specify multiple domains/prefixes in the config

    • Be able to specify which domain/prefix item config to use while constructing the short URL
    • Must be able to reuse the same identifier for the same shortened URL and be reuseable on all domin/prefixes
    • Use default if none is specified
    • Ability to add custom domain/prefixes on the fly

Thank you for this wonderful package and looking forward to your response on this enhancement.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.