PHP code example of datomatic / laravel-active-campaign

1. Go to this page and download the library: Download datomatic/laravel-active-campaign library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

datomatic / laravel-active-campaign example snippets


use Datomatic\ActiveCampaign\Facades\ActiveCampaign;

$contact = ActiveCampaign::contacts()->sync([
    'email' => '[email protected]',
    'firstName' => 'John',
    'lastName' => 'Doe',
]);

ActiveCampaign::contacts()->tag($contact['id'], 5);

return [
    'base_url' => env('ACTIVE_CAMPAIGN_BASE_URL'),

    'api_key' => env('ACTIVE_CAMPAIGN_API_KEY'),

    // Request timeout, in seconds.
    'timeout' => 100,

    // How many times a request is attempted before giving up.
    // Only connection errors, 429 and 5xx responses are retried.
    'retry_times' => 3,

    // How long to wait between two attempts, in milliseconds.
    'retry_sleep' => 1000,

    // Map your ActiveCampaign custom field ids to the names you want to use in your code.
    'custom_fields' => [
        // 'is_email_verified' => 50,
    ],
];

ActiveCampaign::tags()->list();                    // Collection<int, array> — first page only
ActiveCampaign::tags()->list('filters[tagType]=contact');
ActiveCampaign::tags()->get(1);                    // array
ActiveCampaign::tags()->create([...]);             // array
ActiveCampaign::tags()->update(1, [...]);          // array
ActiveCampaign::tags()->delete(1);                 // void

ActiveCampaign::contacts()->list(limit: 100, offset: 200); // one page, explicitly
ActiveCampaign::contacts()->count();                       // total matching records
ActiveCampaign::contacts()->paginate(perPage: 50);         // LengthAwarePaginator, for views
ActiveCampaign::contacts()->lazy();                        // LazyCollection over every page
ActiveCampaign::contacts()->all();                         // Collection over every page

ActiveCampaign::contacts()->lazy()
    ->filter(fn (array $contact) => $contact['email'])
    ->each(fn (array $contact) => ProcessContact::dispatch($contact));

$contacts = ActiveCampaign::contacts()->paginate(perPage: 50);

$contacts->total();       // from the API's meta.total
$contacts->lastPage();
$contacts->items();

ActiveCampaign::contacts()->count('filters[created_after]=2024-01-01');
ActiveCampaign::contacts()->all('filters[created_after]=2024-01-01');

use Datomatic\ActiveCampaign\Enums\FilterOperator;
use Datomatic\ActiveCampaign\Support\Query;

$contacts = ActiveCampaign::contacts()->all(
    Query::make()
        ->filter('created_after', new DateTimeImmutable('-30 days'), FilterOperator::GreaterThan)
        ->filter('status', 1)
        ->orderByDesc('cdate')
        ->

use Datomatic\ActiveCampaign\Facades\ActiveCampaign;

// List / search / filter — the query string is passed through to the API
$contacts = ActiveCampaign::contacts()->list('[email protected]');

$contact = ActiveCampaign::contacts()->get(1);

// Create — 'email' is il' => '[email protected]',
    'firstName' => 'John',
]);

ActiveCampaign::contacts()->update(1, ['email' => '[email protected]', 'lastName' => 'Doe']);

ActiveCampaign::contacts()->delete(1);

'custom_fields' => [
    'is_email_verified' => 50,
    'city' => 51,
],

$contact = ActiveCampaign::contacts()->sync([
    'email' => '[email protected]',
    'is_email_verified' => '1',
    'city' => 'Rome',
]);

$contact['city']; // 'Rome'

ActiveCampaign::contacts()->tags(1);        // the contactTag rows of contact 1
ActiveCampaign::contacts()->tag(1, 5);      // apply tag 5 to contact 1
ActiveCampaign::contacts()->untag(1, 5);    // throws if the contact is not tagged
ActiveCampaign::contacts()->tryUntag(1, 5); // no-op if the contact is not tagged

ActiveCampaign::contacts()->getContactTagId(1, 5); // ?int

use Datomatic\ActiveCampaign\Enums\ListStatus;

ActiveCampaign::contacts()->lists(1);   // current subscriptions, with their status

ActiveCampaign::contacts()->updateListStatus(1, [
    2 => ListStatus::Subscribed,
    3 => ListStatus::Unsubscribed,
]);

ActiveCampaign::contacts()->automations(1);              // automations the contact is in
ActiveCampaign::contacts()->addToAutomation(1, 42);
ActiveCampaign::contacts()->removeFromAutomation(1, 42); // throws if the contact is not in it
ActiveCampaign::contacts()->tryRemoveFromAutomation(1, 42);

ActiveCampaign::contacts()->getContactAutomationId(1, 42); // ?int

use Datomatic\ActiveCampaign\Enums\BulkImportStatus;

$result = ActiveCampaign::import()->bulk([
    ['email' => '[email protected]', 'firstName' => 'Jane', 'city' => 'Rome'],
    ['email' => '[email protected]', 'tags' => ['customer'], 'subscribe' => [1, 2]],
]);

$result['batchId']; // "0641fbdd-..."

$batches = ActiveCampaign::import()->bulkAll(
    User::lazy()->map(fn (User $user) => ['email' => $user->email, 'firstName' => $user->name]),
);

$status = ActiveCampaign::import()->statusOf($result['batchId']); // ?BulkImportStatus

if ($status?->isFinished()) {
    $info = ActiveCampaign::import()->status($result['batchId']);

    $info['success']; // ids of the contacts created
    $info['failure']; // emails the API rejected
}

ActiveCampaign::import()->info(); // outstanding and recently completed batches, account wide

ActiveCampaign::lists()->list();
ActiveCampaign::lists()->list('filters[name]=Newsletter');
ActiveCampaign::lists()->get(1);

ActiveCampaign::lists()->createList(
    name: 'Newsletter',
    stringId: 'newsletter',
    senderUrl: 'https://example.com',
    senderReminder: 'You subscribed on our website.',
);

ActiveCampaign::lists()->delete(1);

ActiveCampaign::automations()->list();
ActiveCampaign::automations()->get(42);

ActiveCampaign::contactAutomations()->add(1, 42);   // same as contacts()->addToAutomation()
ActiveCampaign::contactAutomations()->list('filters[automation]=42');

$pipeline = ActiveCampaign::pipelines()->createPipeline('Qualifications', [
    'currency' => 'eur',
    'autoassign' => 1,
]);

$stage = ActiveCampaign::dealStages()->createStage('Initial contact', $pipeline['id'], [
    'order' => 1,
    'color' => '32B0FC',
]);

$deal = ActiveCampaign::deals()->createDeal(
    title: 'New business',
    value: 150000,          // in cents
    currency: 'eur',        // 3-letter ISO code
    groupId: $pipeline['id'],
    stageId: $stage['id'],
    ownerId: 1,
    contactId: 7,
);

$account = ActiveCampaign::accounts()->createAccount('Example Ltd', [
    'accountUrl' => 'https://example.com',
    'owner' => 1,
]);

ActiveCampaign::accounts()->addContact($account['id'], 7, 'Product Manager');
ActiveCampaign::accountContacts()->list('filters[account]='.$account['id']);

use Datomatic\ActiveCampaign\Enums\NoteRelType;

ActiveCampaign::notes()->createNote('Called them back', 7);                        // a contact
ActiveCampaign::notes()->createNote('Budget approved', $deal['id'], NoteRelType::Deal);
ActiveCampaign::notes()->createNote('Renewal in March', $account['id'], NoteRelType::Account);

use Datomatic\ActiveCampaign\Enums\TagType;

ActiveCampaign::tags()->list();
ActiveCampaign::tags()->createTag('customer', 'a paying user');
ActiveCampaign::tags()->createTag('header', '', TagType::Template);
ActiveCampaign::tags()->updateTag(1, 'customer', 'updated description');
ActiveCampaign::tags()->delete(1);

use Datomatic\ActiveCampaign\Enums\FieldType;

ActiveCampaign::fields()->list();

ActiveCampaign::fields()->createField('City');
ActiveCampaign::fields()->createField('Birthday', FieldType::Date, [
    'perstag' => 'BIRTHDAY',
    'visible' => 1,
]);

ActiveCampaign::fields()->updateField(1, 'Town', FieldType::Text);
ActiveCampaign::fields()->delete(1);

$field = ActiveCampaign::fields()->createField(
    'Department',
    FieldType::DropDown,
    options: ['Sales', 'Engineering', 'Support'],
    lists: [1, 2],
);

ActiveCampaign::fields()->createOptions(34, ['Sales', 'Engineering']);
ActiveCampaign::fields()->options(34);      // Collection of the field's options

ActiveCampaign::fields()->relate(34, 1);    // relate field 34 to list 1
ActiveCampaign::fields()->relate(34);       // relate it to every list
ActiveCampaign::fields()->relations(34);    // Collection of the field's list relations

ActiveCampaign::fields()->createOptions(34, [
    ['value' => 'sales', 'label' => 'Sales', 'isdefault' => true],
    ['value' => 'eng', 'label' => 'Engineering'],
]);

ActiveCampaign::fieldOptions()->createMany([
    ['field' => 34, 'value' => 'Sales', 'label' => 'Sales', 'orderid' => 1],
]);

ActiveCampaign::fieldRels()->relate(34, 1);

// Set custom field 3 of contact 7
ActiveCampaign::fieldValues()->createFieldValue(7, 3, 'Rome');

ActiveCampaign::fieldValues()->updateFieldValue(1, 3, 'Milan');

ActiveCampaign::fieldValues()->list('filters[fieldid]=3');
ActiveCampaign::fieldValues()->delete(1);

use Datomatic\ActiveCampaign\Exceptions\ActiveCampaignException;

try {
    ActiveCampaign::contacts()->get(999999);
} catch (ActiveCampaignException $e) {
    // The request to "contacts/999999" generated this error: [{"title":"No Result found ..."}]
    report($e);
}

use Datomatic\ActiveCampaign\Enums\Method;

$lists = ActiveCampaign::contacts()->request(
    method: Method::GET,
    path: 'lists',
    responseKey: 'lists',
);

use Datomatic\ActiveCampaign\Contracts\ActiveCampaignClientContract;

$response = resolve(ActiveCampaignClientContract::class)
    ->send(Method::GET, 'campaigns'); // Illuminate\Http\Client\Response

use Datomatic\ActiveCampaign\Enums\Method;
use Datomatic\ActiveCampaign\Testing\ActiveCampaignFake;

ActiveCampaignFake::fake([
    'contacts' => ActiveCampaignFake::list('contacts', [
        ['id' => '1', 'email' => '[email protected]'],
    ], total: 42),

    'contacts/1' => ActiveCampaignFake::single('contact', ['id' => '1']),

    'contacts/2' => ActiveCampaignFake::error(['No Result found'], 404),
]);

// ... exercise your code ...

ActiveCampaignFake::assertSent(Method::POST, 'contactTags');
ActiveCampaignFake::assertSentJson(Method::POST, 'contactTags', [
    'contactTag' => ['contact' => 1, 'tag' => 5],
]);
ActiveCampaignFake::assertNotSent(Method::DELETE, 'contacts/*');
ActiveCampaignFake::assertSentCount(2);
bash
php artisan vendor:publish --tag="active-campaign-config"