Personnalized suggestions

This section shows how to use the Ownpage API to display suggestions.

Get suggestions

To get one reader's list of suggestions:

GET https://recos.ownpage.fr/v1/clients/BLOCK_KEY/users/USER_ID/recommendations?rows=NUMBER_OF_RECOS

With :

Optionnal parameter :

Example : to get the six first suggestions for the user 123456 excluding the url http://www.canardmag.com/article-123.html and http://www.canardmag.com/article-456.html :

GET https://recos.ownpage.fr/v1/clients/abcdef/users/123456/recommendations?rows=6&excluded[]=http%3A%2F%2Fwww.canardmag.fr%2Farticle-123.html&excluded[]=http%3A%2F%2Fwww.canardmag.fr%2Farticle-456.html

Example of return in JSON:

{
    "count": 100,
    "start": 0,
    "recommendations": [
        {
            "guid": "123456",
            "url": "http://www.canardmag.fr/news/Fabian-frei-a-mayence/568746",
            "title": "Fabian Frei à Mayence",
            "desc": "Le Suisse Fabian Frei quitte le FC Bâle pour rejoindre Mayence. Le ...",
            "image": "http://medias.canardmag.fr/img-photo-jpg/fabian-frei-a-disput-[...].jpg",
            "category": "Sport"
        },
        {
            "guid": "...",
            "url": "...",
            "title": "...",
            "desc": "...",
            "image": "...",
            "category": "..."
        }
    ]
}

Display infos from your Facebook page

If your block is configured to include facebook infos (email us at support@ownpage.fr), only articles published on your facebook page will be recommended. Facebook infos are available in the API in the additional_data key. To display the title or image of the article, you can choose between the one used on your site and the one used on your facebook page.

Note that the same article can be published on mutiple facebook pages, so in the JSON, the facebook key is an array.

Example of return in JSON:

{
    "count": 100,
    "start": 0,
    "recommendations": [
        {
            "guid": "123456",
            "url": "http://www.canardmag.fr/news/Fabian-frei-a-mayence/568746",
            "title": "Fabian Frei à Mayence",
            "desc": "Le Suisse Fabian Frei quitte le FC Bâle pour rejoindre...",
            "image": "http://medias.canardmag.fr/img-photo-jpg/fabian-frei-[...].jpg",
            "category": "Sport",
            "additional_data": {
              "facebook": [{
                "feed": "CanardMag",
                "feed_logo": "http://medias.canardmag.fr/logo.jpg",
                "image": "http://medias.canardmag.fr/img-photo-jpg/fabian-frei-[...].jpg",
                "url": "http://www.canardmag.fr/news/Fabian-frei-a-mayence/568746",
                "message": "Message facebook du community manager",
                "name": "Fabian Frei à Mayence",
                "description": "Le Suisse Fabian Frei quitte le FC Bâle pour rejoindre...",
                "reactions": {
                  "like": 15,
                  "comment": 7,
                  "share": 11,
                  "wow": 3,
                  "haha": 1,
                  "sad": 2,
                  "love": 0,
                  "angry": 10,
                  "thankful": 1
                }
              }]
            }
        }
    ]
}

Anonymous readers

For not identified readers, the USER_ID variable should be filled by the Ownpage cookie. This cookie is accessible with the Ownpage JS library :

<script type="text/javascript">
(function(){
    // Chargement de la librairie Ownpage
    function loadOwnpageScript(e){var t="https://script.ownpage.fr/v1/",a=document.createElement("script"),n=document.getElementsByTagName("script")[0];a.type="text/javascript",a.defer=1,a.async=1,a.src=t+"ownpage.js",a.onload=e,a.readyState?a.onreadystatechange=function(){("loaded"==a.readyState||"complete"==a.readyState)&&(a.onreadystatechange=null,e&&"function"==typeof e&&e())}:a.onload=function(){e&&"function"==typeof e&&e()},n.parentNode.insertBefore(a,n)}

    loadOwnpageScript(function () {
       var userId = Ownpage.getAnonymousUserId();
    });
})();
</script>

Display (JSONP)

To display suggestions with the Ownpage JS library (format JSONP):

<script type="text/javascript">
(function(){
    // Chargement de la librairie Ownpage
    function loadOwnpageScript(e){var t="https://script.ownpage.fr/v1/",a=document.createElement("script"),n=document.getElementsByTagName("script")[0];a.type="text/javascript",a.defer=1,a.async=1,a.src=t+"ownpage.js",a.onload=e,a.readyState?a.onreadystatechange=function(){("loaded"==a.readyState||"complete"==a.readyState)&&(a.onreadystatechange=null,e&&"function"==typeof e&&e())}:a.onload=function(){e&&"function"==typeof e&&e()},n.parentNode.insertBefore(a,n)}

    loadOwnpageScript(function () {
       Ownpage.getRecommendations(BLOCK_KEY, USER_ID, DISPLAY_FUNCTION_NAME, NUMBER_OF_RECOS, EXCLUDED_ARTICLES);
    });
})();
</script>

With :

The display function is a javascript function called when the suggestions are returned by the API. This function should be accessible in the global scope. The unique parameter of the function is the array of suggestions computed for the user.

Example of display function :

function displayRecommendations(recommendations) {
    var recommendations = recommendations.result.recommendation
    for (var i = 0; i < recommendations.length; i++) {
        var recommendation = recommendations[i]
        // Affichage de la recommandation
    }
}

Complete example (with Ownpage JS library)

HTML page for the user 123456 with tracking and suggestion display :

<!DOCTYPE html>
<html>
<head>
    <title>Sample recommendations</title>
</head>
<body>

<ul id="ownpage-recommendations"></ul>

<script type="text/javascript">
// Display function  should be in the global scope
function displayRecommendations(recommendations) {
    var recommendations = recommendations.result.recommendation;
    for (var i = 0; i < recommendations.length; i++) {
        var list = document.getElementById("ownpage-recommendations");
        var newLI = document.createElement("li");
        var newText = document.createTextNode(recommendations[i].title);
        newLI.appendChild(newText);
        list.appendChild(newLI);
    }
}

(function(){
    // Ownpage library loading
    function loadOwnpageScript(e){var t="https://script.ownpage.fr/v1/",a=document.createElement("script"),n=document.getElementsByTagName("script")[0];a.type="text/javascript",a.defer=1,a.async=1,a.src=t+"ownpage.js",a.onload=e,a.readyState?a.onreadystatechange=function(){("loaded"==a.readyState||"complete"==a.readyState)&&(a.onreadystatechange=null,e&&"function"==typeof e&&e())}:a.onload=function(){e&&"function"==typeof e&&e()},n.parentNode.insertBefore(a,n)}

    loadOwnpageScript(function () {
        Ownpage.trackPage(CLIENT_TRACKING_KEY, 123456);
        Ownpage.getRecommendations(BLOCK_KEY, 123456, "displayRecommendations", 10);
    });
})();
</script>
</body>
</html>