Skip to content

Integrating and Updating Entities

This page offers examples on integrating and updating products and content entities in Relewise.

The examples below are using our PHP SDK, which is available via Packagist.

To make requests with the SDK you need to use the types from our PHP SDK. The types are located in the namespace Relewise\Models and an example of using a concrete type could be the following:

php
use Relewise\Models\TrackProductViewRequest;

Read more about how to authenticate against our API here.

Integrating and Updating Products with PHP

To get products into Relewise, follow the template laid out below. Be aware of different data types and when to use the correct one.

Add whatever fields are necessary for your integration to be useful for your search and/or recommendation requirements.

If you are unsure of what entity-actions to take, refer to our list of entity-actions.


To update or import the details of a single product you can make a TrackProductUpdateRequest.

The following request uses ProductUpdateUpdateKind::ReplaceProvidedProperties which creates a new product if the product hasn't been tracked before and else updates the properties set in the request for that product.

php
public function entryPoint(): void
{
	// Copy dataset id + API key + server URL from 
	// my.relewise.com > Administration -> API Keys
    $tracker = new Tracker("00000000-0000-0000-0000-000000000001", "your api key");
    $tracker->serverUrl = "the server URL for the dataset";

    // This is the recommended "Push" method, for importing product data into Relewise.
    $this->fullProductUpdates($tracker);
}

private function fullProductUpdates(Tracker $tracker): void
{

    // Create a timestamp to distinguish active and inactive entities
    // Read more at
    // https://docs.relewise.com/docs/developer/bestpractices/product-integration.html
    $importedAtTimestamp = time();
    
    // Language can be any string, and doesnt have to be a valid iso-standard.
    $english = Language::create("en"); 
    $dkk = Currency::create("DKK");

    $productUpdates = array();

    // Foreach product that needs to be imported, do the following:
    {
        // The Id should be the primary id of the product
        $product = Product::create("Product-SKU-01");

        // We only set the English translation in this example 
		// but more can be set by parsing more MultilingualValue 
		// to the Multilingual::create method.
        $product->setDisplayName(Multilingual::create(
            MultilingualValue::create($english, "The english display name")));
        
        $product->setSalesPrice(MultiCurrency::create(Money::create($dkk, 199)));
        $product->setListPrice(MultiCurrency::create(Money::create($dkk, 199)));

        $product->setBrand(
            // Displayname can be left out, but Id is required
            Brand::create("brandId")
                ->setDisplayName("Brand display name") 
        );

        // We only set the English translation in this example 
		// but more can be set by parsing more MultilingualValue 
	    // to the Multilingual::create method. 
        $product->addToData("ShortDescription", DataValueFactory::multilingual(
            Multilingual::create(MultilingualValue::create(
                $english, "The short english description"))));

        $product->addToCategoryPaths(CategoryPath::create(
            CategoryNameAndId::create("74", Multilingual::create(
                MultilingualValue::create($english, "Play"))),
            CategoryNameAndId::create("2", Multilingual::create(
                MultilingualValue::create($english, "Swings"))),
            CategoryNameAndId::create("529", Multilingual::create(
                MultilingualValue::create($english, "Swing Seats")))
        ));

        $product->addToData("ImportedAt", DataValueFactory::float(
            $importedAtTimestamp));
        $product->addToData("MinimumAge", DataValueFactory::float(4));
        $product->addToData("InStock", DataValueFactory::boolean(true));
        $product->addToData("USPs", DataValueFactory::stringListFromArray(
            array("first usp", "second usp", "third usp")));
		// Add any additional fields you would want returned from Relewise

        $variants = array();
        // Foreach variant of this product
        {
            // Variant Id only has to be unique within the product
            $variant = ProductVariant::create("The variant id"); 
            
            $variant->addToData("Materials", DataValueFactory::multilingualCollection(
                array(array(
                    "Language" => $english,
                    "Values" => array("Wood", "Metal")
                )
            )));
            $variant->addToData("Colors", DataValueFactory::multilingualCollection(
                array(array(
                    "Language" => $english,
                    "Values" => array("Red", "Green")
                )
            )));

            $variant->addToData("PrimaryMaterial", DataValueFactory::multilingual(
                Multilingual::create(MultilingualValue::create($english, "Wood"))));
            $variant->addToData("PrimaryColor", DataValueFactory::multilingual(
                Multilingual::create(MultilingualValue::create($english, "Red"))));
		    
            // Add any additional fields you would want returned from Relewise

            array_push($variants, $variant);
        }

        array_push($productUpdates, ProductUpdate::create(
            $product, $variants,
            productUpdateKind: ProductUpdateUpdateKind::ReplaceProvidedProperties,
            variantUpdateKind: ProductUpdateUpdateKind::ReplaceProvidedProperties,
            replaceExistingVariants: true 
        // Replace existing variants = true will delete all variants in Relewise
		// (for the listed products) not included in this update request.
        ));
    }
    // Foreach product END

    // If this is a full-import (not delta), disable all non-included products
    {
        $nonUpdatedProductFilter = FilterCollection::create(
            ProductDataFilter::create("ImportedAt")
            ->setConditions(ValueConditionCollection::create(
                EqualsCondition::create(DataValueFactory::float(
                    $importedAtTimestamp), negated: true)
            ))
        );
        array_push($productUpdates, ProductAdministrativeAction::create(
            Language::UNDEFINED, 
            Currency::UNDEFINED, 
            $nonUpdatedProductFilter, 
            ProductAdministrativeActionUpdateKind::Disable, 
            ProductAdministrativeActionUpdateKind::Disable));
    }

    $updatedProductFilter = FilterCollection::create(
        ProductDataFilter::create("ImportedAt")
        ->setConditions(ValueConditionCollection::create(
            EqualsCondition::create(DataValueFactory::float(
                $importedAtTimestamp), negated: false)
        ))
    );
    array_push($productUpdates, ProductAdministrativeAction::create(
        Language::UNDEFINED, 
        Currency::UNDEFINED, 
        $updatedProductFilter, 
        ProductAdministrativeActionUpdateKind::Enable, 
        ProductAdministrativeActionUpdateKind::Disable));

	// Send all product updates and actions as a single (or few) network request.
	// The client will automatically send batches of 1.000 items per network request, 
	// so you can pass as many as you like to the tracker.Track / TrackAsync method.
    $tracker->batchedTracking(
        BatchedTrackingRequest::create()->setItemsFromArray($productUpdates));
}

Integrating and Updating Content with PHP

To get content into Relewise, follow the template laid out below. Be aware of different data types and when to use the correct one.

Add whatever fields are necessary for your integration to be useful for your search and/or recommendation requirements.

If you are unsure of what entity-actions to take, refer to our list of entity-actions.

php
public function entryPoint(): void
{
	// Copy dataset id + API key + server URL from 
	// my.relewise.com > Administration -> API Keys   
    $tracker = new Tracker("00000000-0000-0000-0000-000000000001", "your api key");
    $tracker->serverUrl = "the server URL for the dataset";

    // This is the recommended "Push" method, for importing content data into Relewise
    $this->fullContentUpdates($tracker);
}

private function fullContentUpdates(Tracker $tracker): void
{
    // Create a timestamp to distinguish active and inactive entities
    // Read more at
    // https://docs.relewise.com/docs/developer/bestpractices/product-integration.html
    $importedAtTimestamp = time();

    // Language can be any string, and doesn't have to be a valid iso-standard.
    $english = Language::create("en"); 
    
    $contentUpdates = array();

    // Foreach content element that needs to be imported, do the following:
    {
        // The Id should be the primary id of the content
        $content = Content::create("Content-ID-01"); 
        // We only set the English translation in this example 
	    // but more can be set by parsing more MultilingualValue 
		// to the Multilingual::create method.
        $content->setDisplayName(Multilingual::create(
            MultilingualValue::create($english, "The English display name")));

        // We only set the English translation in this example 
	    // but more can be set by parsing more MultilingualValue 
		// to the Multilingual::create method.
        $content->addToData("ShortDescription", DataValueFactory::multilingual(
            Multilingual::create(MultilingualValue::create(
                $english, "The short English description"))));

        $content->addToCategoryPaths(CategoryPath::create(
            CategoryNameAndId::create("23", Multilingual::create(
                MultilingualValue::create($english, "Outdoor"))),
            CategoryNameAndId::create("372", Multilingual::create(
                MultilingualValue::create($english, "Hiking")))
        ));

        $content->addToData("ImportedAt", DataValueFactory::float(
            $importedAtTimestamp));
        $content->addToData("ReadingTimeInMinutes", DataValueFactory::float(3));
        $content->addToData("News", DataValueFactory::boolean(true));
        $content->addToData("Badges", DataValueFactory::stringListFromArray(array(
            "fun", "current season", "some other badge")));
            
		// Add any additional fields you would want returned from Relewise

        array_push($contentUpdates, ContentUpdate::create(
            $content,
            kind: ContentUpdateUpdateKind::ReplaceProvidedProperties
        ));
    }
    // Foreach content element END

    // If this is a full-import (not delta), disable all non-included content elements
    {
        $nonUpdatedContentFilter = FilterCollection::create(
            ContentDataFilter::create("ImportedAt")
            ->setConditions(ValueConditionCollection::create(
                EqualsCondition::create(DataValueFactory::float(
                    $importedAtTimestamp), negated: true)
            ))
        );
        array_push($contentUpdates, ContentAdministrativeAction::create(
            Language::UNDEFINED, 
            Currency::UNDEFINED, 
            $nonUpdatedContentFilter, 
            ContentAdministrativeActionUpdateKind::Disable));
    }

    $updatedContentFilter = FilterCollection::create(
        ContentDataFilter::create("ImportedAt")
        ->setConditions(ValueConditionCollection::create(
            EqualsCondition::create(DataValueFactory::float(
                $importedAtTimestamp), negated: false)
        ))
    );
    array_push($contentUpdates, ContentAdministrativeAction::create(
        Language::UNDEFINED, 
        Currency::UNDEFINED, 
        $updatedContentFilter, 
        ContentAdministrativeActionUpdateKind::Enable));

	// Send all content updates and actions as a single (or few) network request.
	// The client will automatically send batches of 1.000 items per network request, 
	// so you can pass as many as you like to the tracker.Track / TrackAsync method.
    $tracker->batchedTracking(
        BatchedTrackingRequest::create()->setItemsFromArray($contentUpdates));
}

Don't know us? Don't worry - you can find more information about us, by visiting our main page www.relewise.com