combindma/laravel-linkedin-insight-tag

LinkedIn Insight Tag integration for Laravel

1.0.0 2022-03-08 19:05 UTC

This package is auto-updated.

Last update: 2024-11-09 01:28:07 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

A complete Laravel package for LinkedIn Conversion Tracking

Installation

You can install the package via composer:

composer require combindma/laravel-linkedin-insight-tag

You can publish the config file with:

php artisan vendor:publish --tag="linkedin-insight-tag-config"

This is the contents of the published config file:

return [
    /*
     * LinkedIn Partner Id id provided by LinkedIn
     */
    'linkedin_partner_id' => env('LINKEDIN_PARTNER_ID', ''),

    /*
     * The key under which data is saved to the session with flash.
     */
    'sessionKey' => env('LINKEDIN_SESSION_KEY', config('app.name').'_linkedinInsightTag'),

    /*
     * Enable or disable script rendering. Useful for local development.
     */
    'enabled' => env('LINKEDIN_INSIGHT_TAG_ENABLED', false),
];

If you plan on using the flash-functionality you must install the LinkedinInsightTagMiddleware, after the StartSession middleware:

// app/Http/Kernel.php
protected $middleware = [
    ...
    \Illuminate\Session\Middleware\StartSession::class,
    \Combindma\LinkedinInsightTag\LinkedinInsightTagMiddleware::class,
    ...
];

Usage

Include scripts in Blade

Insert head view after opening head tag, and body view after opening body tag

<!DOCTYPE html>
<html>
<head>
    @include('linkedinInsightTag::head')
</head>
<body>
@include('linkedinInsightTag::body')
</body>

Your conversion events will also be rendered here. To add an event, use the conversion() function.

// HomeController.php
use Combindma\LinkedinInsightTag\Facades\LinkedinInsightTag;

public function index()
{
    LinkedinInsightTag::conversion('7652529'); //your conversion_id provided by Linkedin
    return view('home');
}

This renders:

<html>
  <head>
    <script>/* Linkedin Insight Tag's base script */</script>
    <!-- ... -->
  </head>
  <body>
  <script>window.lintrk('track', { conversion_id: 7652529 });</script>
  <!-- ... -->
</html>

Flashing data for the next request

The package can also set event to render on the next request. This is useful for setting data after an internal redirect.

// ContactController.php
use Combindma\LinkedinInsightTag\Facades\LinkedinInsightTag;

public function postContact()
{
    // Do contact form stuff...
    LinkedinInsightTag::flashConversion('7652529');
    return redirect()->action('ContactController@getContact');
}

After a form submit, the following event will be parsed on the contact page:

<html>
<head>
    <script>/* Linkedin Insight Tag's base script */</script>
    <!-- ... -->
</head>
<body>
<script>window.lintrk('track', { conversion_id: 7652529 });</script>
<!-- ... -->
</html>

Other Simple Methods

use Combindma\LinkedinInsightTag\Facades\LinkedinInsightTag;

// Retrieve your Partner id
$id = LinkedinInsightTag::partnerId(); // XXXXXXXX
// Check whether script rendering is enabled
$enabled = LinkedinInsightTag::isEnabled(); // true|false
// Enable and disable script rendering on the fly
LinkedinInsightTag::enable();
LinkedinInsightTag::disable();
// Add conversion event to the conversion layer (automatically renders right before the tag script). Setting new values merges them with the previous ones.
LinkedinInsightTag::conversion(123456); //only int values
// Flash event for the next request. Setting new values merges them with the previous ones.
LinkedinInsightTag::flashConversion(123456);
//Clear the conversion layer.
LinkedinInsightTag::clear();

Macroable

Adding conversion events to pages can become a repetitive process. Since this package isn't supposed to be opinionated on what your events should look like, the LinkedinInsightTag is macroable.

use Combindma\LinkedinInsightTag\Facades\LinkedinInsightTag;

//include this in your macrobale file
LinkedinInsightTag::macro('purchase', function () {
    LinkedinInsightTag::conversion(123456);
    LinkedinInsightTag::conversion(654321);
});

//in your controller
LinkedinInsightTag::purchase();

Testing

composer test

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.