Integration code examples

Below are code examples of generating the parselypage metatag in different programming languages and content management systems. All of the examples use following sample post:

Variable Value
Title Zipf's Law of the Internet: Explaining Online Behavior
Link https://blog.parse.ly/post/57821746552
Image URL https://blog.parse.ly/inline_mra670hTvL1qz4rgp.png
Type post
Post ID 57821746552
Pub Date 2013-08-15 at 13:00:00 EST
Section Programming
Author Alan Alexander Milne
Tags statistics, zipf, internet, behavior

Notice that the title has an apostrophe in the first word that could be troublesome for JSON documents. In addition, the date the article was published is given in eastern standard time (EST) where as JSON-LD requires UTC time.

WordPress

Use wp-parsely WordPress plugin to set up tracking for your WordPress installation. Follow the installation instructions for how to set it up.

PHP

<?php
$title      = "Zipf's Law of the Internet: Explaining Online Behavior";
$link       = "https://blog.parse.ly/post/57821746552";
$imageURL   = "https://blog.parse.ly/inline_mra670hTvL1qz4rgp.png";
$section    = "Programming";
$author     = "Alan Alexander Milne";
$tags       = array("Programming", "traffic", "local");
$pubDate    = DateTime::createFromFormat("Y-m-d H:i:s P", "2012-01-01 11:34:02 -05:00");

function getCleanParselyPageValue($val) {
    $val = str_replace("\n", "", $val);
    $val = str_replace("\r", "", $val);
    return $val;
}

$parselyPage = array();
$parselyPage["title"]       = getCleanParselyPageValue($title);
$parselyPage["link"]        = $link;
$parselyPage["image_url"]   = $imageURL;
$parselyPage["type"]        = "post";
$parselyPage["post_id"]     = "57821746552";
$parselyPage["pub_date"]    = gmdate("Y-m-d\TH:i:s\Z", $pubDate->getTimestamp());
$parselyPage["section"]     = getCleanParselyPageValue($section);
$parselyPage["author"]      = getCleanParselyPageValue($author);
$parselyPage["tags"]        = $tags;

$output = "<script type=\"application/ld+json\"> " . json_encode($parselyPage, JSON_HEX_APOS | JSON_HEX_QUOT) . " </script>";
?>

Python

import datetime
import json
import pytz

def get_clean_parsely_page_value(string):
    return string.replace('\n', ' ').replace("'", "\u0027")


title       = "Zipf's Law of the Internet: Explaining Online Behavior"
link        = "https://blog.parse.ly/post/57821746552"
image_url   = "https://blog.parse.ly/inline_mra670hTvL1qz4rgp.png"
section     = "Programming"
author      = "Alan Alexander Milne"
pub_date    = datetime.datetime(2012, 1, 1, 11, 34, 2, tzinfo=pytz.timezone("US/Eastern"))
tags        = ["statistics", "zipf", "internet", "behavior"]

parsely_page = {
    "title": get_clean_parsely_page_value(title),
    "link": link,
    "image_url": image_url,
    "type": "post",
    "post_id": "57821746552",
    "pub_date": pub_date.astimezone(pytz.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
    "section": get_clean_parsely_page_value(section),
    "author": get_clean_parsely_page_value(author),
    "tags": tags
}

output = '<script type="application/ld+json"> %s </script>' % json.dumps(parsely_page)

Ruby

require 'date'

title       = "Zipf's Law of the Internet: Explaining Online Behavior"
link        = "https://blog.parse.ly/post/57821746552"
image_url   = "https://blog.parse.ly/inline_mra670hTvL1qz4rgp.png"
section     = "Programming"
author      = "Alan Alexander Milne"
pub_date    = Time.new(2012,1,1,11,34,2, '-05:00')
tags        = ["statistics", "zipf", "internet", "behavior"]

def get_clean_parsely_page_value(val)
    return val.gsub(/\n/, "").gsub(/'/, "\u0027")

parsely_page = {}
parsely_page[:title]      = get_clean_parsely_page_value(title)
parsely_page[:link]       = link
parsely_page[:image_url]  = image_url
parsely_page[:type]       = "post"
parsely_page[:post_id]    = "57821746552"
parsely_page[:pub_date]   = pub_date.utc.strftime("%FT%TZ")
parsely_page[:section]    = get_clean_parsely_page_value(section)
parsely_page[:author]     = get_clean_parsely_page_value(author)
parsely_page[:tags]       = tags

output = "<script type=\"application/ld+json\"> #{JSON.generate(parsely_page)} </script>"
Do you have an urgent support question?