<?php

namespace App\Tracking;

use App\Facades\Settings;
use Illuminate\Contracts\Support\Arrayable;

class PinterestTracker implements Arrayable
{
    public function toArray()
    {
        $tagId = Settings::get('tracking-pinterest-tag-id');
        return [
            [
                "body" => '!function(e){'
                    . 'if(!window.pintrk){'
                    . 'window.pintrk=function(){window.pintrk.queue.push(Array.prototype.slice.call(arguments))};'
                    . 'var n=window.pintrk;n.queue=[],n.version="3.0";'
                    . 'var t=document.createElement("script");t.async=!0,t.src=e;'
                    . 'var r=document.getElementsByTagName("script")[0];'
                    . 'r.parentNode.insertBefore(t,r)}}'
                    . '("https://s.pinimg.com/ct/core.js");'
                    . 'pintrk("load", "' . $tagId . '");'
                    . 'pintrk("page");'

                    . 'var noscript = document.createElement("noscript");'
                    . 'var img = document.createElement("img");'
                    . 'img.setAttribute("height", "1");'
                    . 'img.setAttribute("width", "1");'
                    . 'img.setAttribute("style", "display:none;");'
                    . 'img.setAttribute("alt", "");'
                    . 'img.setAttribute("src", "https://ct.pinterest.com/v3/?tid=' . $tagId . '&noscript=1");'
                    . 'noscript.appendChild(img);'
                    . 'document.body.appendChild(noscript);',
                'data-consent' => 'marketing'
            ]
        ];
    }
}
